Cross-domain Ajax Request Basic Authentication
I'm making cross-domain ajax request to get some data. The REST service have Basic authentication (set through IIS). $.ajax({ type: 'GET', xhrFields: {
Solution 1:
Pass username and password like following code,
$.ajax({
type: "GET",
xhrFields: {
withCredentials: true
},
dataType: "jsonp",
contentType: "application/javascript",
data: myData,
async: false,
crossDomain: true,
url: "http://xx.xx.xx.xx/MyService/MyService.svc/GetData",
success: function (jsonData) {
console.log(jsonData);
},
error: function (request, textStatus, errorThrown) {
console.log(request.responseText);
console.log(textStatus);
console.log(errorThrown);
}
username: username,
password: password,
});
Solution 2:
$.ajax({
url: 'yoururl',
username : username,
password :password,
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
dataType: "text",
xhrFields:
{
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
}
});
Solution 3:
$.ajax({//No need to pass credentials
type: "get",
async: false,
url: "https://someurl/",
xhrFields: {
withCredentials: true
},
success: function (data) {
//do something
}
})
Post a Comment for "Cross-domain Ajax Request Basic Authentication"