I'm trying to call a REST service that uses Basic Authentication from a JavaScript thats rendered on a SharePoint page. For the Basic Authentication it should use the credentials the user logged into sharepoint with.
I tried to do this using withCredentials = true
, but it doesn't work, the REST service doesn't receive any credentials from the call.
jQuery.support.cors = true;
jQuery.ajax({
url: "https://myurl?and=parameters",
crossDomain: true,
processData: false,
type: "GET",
dataType: "json",
data: {},
beforeSend: function (xhr) {
xhr.withCredentials = true;
},
success: function (data, textStatus, jqXHR) {
alert("Success");
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ": " + jqXHR.status + " / " + jqXHR.responseText + " / " + errorThrown);
}
});
(jQuery is our handle for jQuery 1.7.2)
Is there another way to do this? What am I missing?
EDIT: I have a different behaviour in IE and Firefox and I used a sniffer to check what is happening:
IE - The browser doesn't even try to send a request or preflight ... JavaScript jumps into the error function saying "access denied" without even trying.
Firefox - The browser sends an authenticated request, the server receives it with the correct user and answers. The client receives an OK 200 result with data ... BUT: The JavaScript then jups into the error function anyway just saying "error" (status 0).