在jQuery中的CORS GET请求

时间:2017-08-09 13:42:24

标签: jquery cors

我正在尝试创建一个应该返回一堆json数据的核心请求,但由于某种原因,请求失败。我到目前为止所做的代码如下:

$(document).ready(function () {
var url = 'http://test/v/calls';
$.ajax({
    url: url,
    beforeSend: function (request) {
        request.setRequestHeader("Authorization", "Bearer test");
    },
    success: function (json) {
        console.log("Success", json);
    },
    error: function (textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
    }
});

回复如下:

catcher.js:197 OPTIONS http://test/v/calls 401 (Unauthorized)

XMLHttpRequest cannot load http://test/v/calls. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://some_domain' is therefore not allowed access. The response had HTTP status code 401.

注意:具有授权参数的相同网址在Postman中可以正常使用。看不出有什么问题。

1 个答案:

答案 0 :(得分:0)

您需要在服务器上启用cors。由于邮递员协议处理跨站点脚本的方式,它在Postman中有效。你的服务器是什么构建的?你如何在你的终端添加cors?你可以从两个问题开始。