jQuery Ajax POST Failing - ajax

I can't get my AJAX request to complete successfully. It's for the bearere token on Twitter's API. I have done successfully in a HTTP request client as shown below:
But I can't replicate this in code:
$.ajax({
type: 'POST',
url: 'https://api.twitter.com/oauth2/token',
headers: {
'Authorization':'Basic ' + base64EncodedBearerTokenCredentials,
},
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
data: {
'grant_type': 'client_credentials'
},
error: function(xhr, status, error) {
},
success: function(response){
},
});
I'm getting a 405, but because I have the Authorization header in I can't see the response in Firebug.
What is it?
Thanks.

Related

How to submit Salesforce's Web to Lead Forms with AJAX instead of the default submission?

I am trying to submit my webtolead form, somethinf that is so simple and basic jQuery, But I am getting CORS origin error :
<script>
// https://www.domainName.my.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
function postFormAjax(formData) {
$.ajax({
method: "POST",
type: "POST",
url: "https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
headers: {
'Access-Control-Allow-Origin': '*'
},
data: formData,
processData: false,
contentType: false,
crossOrigin: true,
crossDomain: true,
error: function (jqXHR, textStatus, errorMessage) {
if (textStatus == "error") {
console.log(jqXHR);
console.log(textStatus);
console.log(errorMessage);
}
},
success: function (data, textStatus, xhr) {
console.log(data);
}
});
}
postFormAjax({ first_name: "test", email: "test#gmail.com" })
</script>
This is the error :
Access to XMLHttpRequest at
'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8'
from origin 'null' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.
I allowed localhost on my Salesforce SETUP :
Also I feel like there is something important about this field :
I don't find many clear guides on how to submit webtolead forms via XMLHTTPSREQUEST !!
Any help or indication here would be much appreciated.

Laravel Authentication with Passport,how send token Bearer in ajax header inside laravel project

I can get response from RESTful api when using Postman with Authorization Bearer + token.
But How can I send request with token in ajax (i made it inside my laravel project).
$.ajax({
url: "url",
headers: {
'Authorization': authorizationToken,
},
type: "post",
data: data,
async: false,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (data) {
},
error: function (data, status, error) {
}
});
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN":$('meta[name="csrf-token"]').attractive('content')
"Authorization":"Bearer"+localStorage.getItem('token')
}
});

Add bearer token to ajax POST request in ASP.Net Core

I have a POST method /Home/PostRequest decorated with the [Authorize] attribute in ASP.Net Core. Client-side I have an authenticated user. I want to construct an ajax POST request to call the method, but I'm unsure what to put into the Authorization header. Where are the auth credentials cached, and in what form?
$.ajax({
type: "POST",
url: "/Home/PostRequest",
data: JSON.stringify(request),
headers: {
"Authorization": "** what to put here? **"
}
contentType: "application/json",
dataType: "json",
crossDomain: false,
success: function (response) {
successCallback(response);
},
failure: function (response) {
console.log("Failed");
},
error: function (xhr, response, thrownError) {
console.log("Error");
}
});

400 using Axios to make a post request to Django Rest Framework

I'm working with learning some javascript to make a post to a little rest API I created with django rest. I have a post that works when I was working with jQuery but now I am reimplementing using Vue.js and am hitting a snag. When I do the below post request I get a 400 error and am not sure what I am missing. I would imagine something with my header is incorrect? Any advice would be welcome.
axios({
method: 'post',
url: '/api/matches/',
data: JSON.stringify(data),
headers: {
"X-CSRFToken": csrfToken,
"Content-Type": "application/json"
}
})
jQuery post for reference:
$.ajax({
type: "POST",
url: "/api/matches/",
data: JSON.stringify(final_data),
success: function() {},
error: function(err) {console.log(err)},
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain:false,
beforeSend: function(xhr, settings) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});

Rest API call using Jquery Ajax with authentication token

I want to consume REST api and for that I have only url and the authentication token. I am using Jquery Ajax call for this purpose. I am getting error
Uncaught SyntaxError: Unexpected token :
Below is my code sample
$.ajax({
type: 'GET',
url: "https://xxxx",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
crossDomain: true,
data: { "api_token": "xxxx", "api_token_secret": "xxx" },
success: function (data) {
console.log(data);
},
error: function (data) {
alert("error");
}
});
this code is in jsfile and that js file I am included in index.html page. on document ready I am calling this function.
If I didn't add the datatype : jsonp it is giving error of
XMLHttpRequest cannot load Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value
Can anyone please help me? What is the issue?
This line:
data: { "api_token": "xxxx", "api_token_secret": "xxx" },
should be this:
data: { api_token: "xxxx", api_token_secret: "xxx" },
You shouldn't quote the data keys.

Resources