Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX request failing in jQuery but not in NodeJS

I have a RESTful web service (hosted on a different server via IIS) that returns JSON. The strange thing is the following NodeJS command line test application (not via the web browser, but via the command line) is working fine and returning the JSON:

Working NodeJS App:

var request = require("request");
var btoa = require("btoa");

var uname = "corp\\user.name";
var pword = "password123"

var options = {
    url: "http://192.168.3.142/v1/foo?format=json",
    headers: {
        "Authorization": "Basic " + btoa(uname + ":" + pword)
    }
};

request(options, function(err, response, body) {
    console.log(body);
});

However the following AJAX request fails with:

OPTIONS http://192.168.3.142/v1/foo?format=json 401 (Unauthorized) jquery-1.11.0.min.js:4
XMLHttpRequest cannot load http://192.168.3.142/v1/foo?format=json. Invalid HTTP status code 401 

This is the response header from the server:

Response Headers:

Access-Control-Allow-Headers:Authorization
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Content-Length:1293
Content-Type:text/html
Date:Thu, 06 Mar 2014 05:41:24 GMT
Server:Microsoft-IIS/7.5
WWW-Authenticate:Basic realm="192.168.3.142"
X-Powered-By:ASP.NET

AJAX code:

$.ajaxSetup({
    beforeSend: function (xhr, settings) {
        var creds = {
            username: "corp\\user.name",
            password: "password123"
        };
        xhr.setRequestHeader("Authorization", "Basic " + btoa(creds.username + ":" + creds.password));
        return true;
    }
});

$.ajax({
    type: "GET",
    url: "http://192.168.3.142/v1/foo?format=json",
    success: function (data, text) {
        console.log(data);
    }
});

UPDATE:

Throws the same 401 (Unauthorized):

var creds = {
    username: "corp\\user.name",
    password: "password123"
};
var credentials = btoa(creds.username + ":" + creds.password);
$.ajax({
    type: "GET",
    dataType: "text/json",
    url: "http://192.168.3.142/v1/foo?format=json",
    beforeSend: function (xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + credentials);
        return true;
    },
    success: function (data, text) {
        console.log(data);
    }
});
like image 528
fulvio Avatar asked Jan 26 '26 00:01

fulvio


1 Answers

Once I added xhrFields: { withCredentials: true } to the $.ajaxSetup({}); the error was returning:

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials.

I added Access-Control-Allow-Credentials: true on the server-side and it's now working correctly.

var creds = {
    username: "username",
    password: "password"
};
var credentials = btoa(creds.username + ":" + creds.password);
$.ajaxSetup({
    xhrFields: { withCredentials: true },
    beforeSend: function (xhr, settings) {
        xhr.setRequestHeader("Authorization", "Basic " + credentials);
        return true;
    }
});

$.ajax({
    type: "GET",
    url: "http://localhost/v1/service",
    async: true,
    success: function (data, text) {
        console.log(data);
    }
});
like image 127
fulvio Avatar answered Jan 27 '26 12:01

fulvio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!