Rest API call using Jquery Ajax with authentication token - ajax

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.

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");
}
});

Post this $ajax request to REST API

I have this ajax POST code, which works directly on browser, but when i want to make a POST using the same data on Fiddler or SoapUI or using httpClient or HttpWebPost it doesn't work, always returns ERRROR. Below are the Ajax code and Fiddler. Thank you in advance.
$.ajax({
type: "POST",
url: "http://IP/address/post/something",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "text",
data:
{ message: 'testMessage', destination: '8888888888', campaign: 'UATTest', agent: 'TestingAgent'}
,
success: function (resp) {
if (resp != "ERROR") {
} else {
}
},
error: function (e) {
}
});
Update :
Does anybody know how to consume a REST API using FormParam ? how is it differ from JSON and XML ?
params:
#FormParam("destination"), #FormParam("message"), #FormParam("campaign"), #FormParam("agent")

jQuery Ajax POST Failing

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.

Resources