Convert a cURL API call to Ajax - javascript

curl --basic --user {username}:{password} https://www.blueworkslive.com/api/Auth
I have tried to write something like this but it won't work! I keep getting error 400
$.ajax({
url: "https://www.blueworkslive.com/api/Auth",
type: "GET", //This is what you should chage
dataType: "jsonp",
username: "admin", // Most SAP web services require credentials
password: "admin",
processData: false,
contentType: "application/json; charset=utf-8",
success: function () {
alert("success");
console.log("success");
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("error");
console.log(xhr.responseText);
alert(xhr.status);
alert(xhr.responseText);
},
});
And this as well:
{$(document).ready(function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.blueworkslive.com/api/Auth?version=20091212", false);
xhr.setRequestHeader("Authorization", "Basic " + btoa("username.com:password"));
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Access-Control-Allow-Origin: *");
xhr.send("");
alert(xhr.status);
alert(xhr.responseText);
});}
Error: Origin is not allowed by Access-Control-Allow-Origin. I assumed the error was caused due to me running the script from a local server (since the origin was set to null by default) so I deployed it on a app development server so that I had a public origin.The same error occurred for the new origin - even though the origin now was not null anymore but the server provided origin!

$(document).ready(function(){
$(function (){
$.ajax({
url: 'https://www.blueworkslive.com/api/Auth', // url checked ...// error 400 not found url
data: {
//data
username:username,
email:email
},
dataType: 'json',
success: function(data)
{
//result success
}
});
});
});

Related

how can I authenticate to a rest web service by ajax

I want to send username and password to a Rest web service to recover the Token. I tested this code but always sends me to error function
$.ajax({
type: "POST",
url: "http://192.168.1.89:1111/connect/token",
dataType: 'json',
async: false,
data: '{}',
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', btoa(username, password));
},
success: function() {
alert('Thanks for your comment!');
},
error: function(xhr, status, error) {
alert(error);
}
});

CROS Origin API Call issue?

I host my MVC API in server 'A'.
I tried to call API using jquery ajax call from native server (server 'A'). it's working fine as expected.
But, when tried to call API from server 'B'. I got an error.
No 'Access-Control-Allow-Origin' header is present on the requested resource
My ajax call like below
$.ajax({
url: Url,
type: "GET",
data: Data,
contentType: "application/json charset=utf-8",
async: false,
cache: false,
error: function (xhr, ajaxOptions, thrownError) {
console.log("Exception in ajaxRequest - " + xhr.status + ' - ' + thrownError);
},
success: function (data) {
ResultData = $.parseJSON(data);
}
});

Authenticating bigcommerce using zapier and javascript

I am trying to authenticate using javascript through Zapier but I am unable to authenticate. I am using the following code:
var auth = 'Basic **************';
var url = 'https://www.*****.com/api/v2/customers?email=mel****.com';
$.ajax({
url : url,
method : 'GET',
dataType: "json",
contentType: "application/json; charset=utf-8",
async: false,
crossDomain: true,
beforeSend : function(req) {
req.setRequestHeader('Authorization', auth);
},
success: function(result) {
alert('done');
console.log(result);
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
});
However I keep receiving a 401 unauthenticated user error. I have tried running it from an https site but can't get it to work. Any pointers would be greatly appreciated.
You need to base64 encode your username:password credentials.
var auth = 'Basic ' +window.btoa('username:password');

Ajax POST request to new Google Analytics V4 API

Google Analytics v4 API was just released and GET requests were changed to POST requests. And there are no good examples out there yet...
So I've successfully received accessToken, but when I try the following POST request - I'm always getting empty object Object { }, but I'm sure that data is there and ViewID is correct!
Any advice helps! Thank you!
requestAnalyticsData1 = function (accessToken) {
var url = "https://analyticsreporting.googleapis.com/v4/reports:batchGet?";
url += "access_token="+accessToken;
var params = {
"reportRequests":[{
"viewId":"121238102",
"dateRanges":[{
"startDate":"yesterday",
"endDate":"today"
}],
"metrics":[{
"expression":"ga:users"
}],
"dimensions": [{
"name":"ga:pagePath"
}]
}]
}
$.ajax({
url: url,
type: "POST",
data: params,
dataType: "json",
success: function(results) {
console.log(results)
parseAnalyticsReportsData(results);
},
error: function(xhr, ajaxOptions, thrownError) {
alert('failed');
alert(xhr.status);
alert(thrownError);
}
});
};
Solution was to replace this part:
data: params,
dataType: "json",
With this:
data: JSON.stringify(params),
dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Content-Type", "application/json");
},

Send headers in JQuery Ajax Get Request

Here is my code.
$.ajax(this.url, {
type: "GET",
//dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("x-token", token)
},
success: function (data) {
console.log(data);
},
error: function(){
console.log('error');
}
});
I am not able to send ajax request by that code. I also try hearder: {"x-token": token}, In place of beforeSend: but its also not working for me.

Categories

Resources