简体   繁体   English

AJAX请求在jQuery中失败但在NodeJS中失败

[英]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. 我有一个RESTful Web服务(通过IIS托管在不同的服务器上)返回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: 奇怪的是以下NodeJS命令行测试应用程序(不是通过Web浏览器,但通过命令行)工作正常并返回JSON:

Working NodeJS App: 工作NodeJS应用程序:

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: 但是,以下AJAX请求失败:

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: AJAX代码:

$.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) : 投掷相同的401(未经授权)

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

Once I added xhrFields: { withCredentials: true } to the $.ajaxSetup({}); 一旦我将xhrFields: { withCredentials: true }$.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. 我在服务器端添加了Access-Control-Allow-Credentials: true ,现在它正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM