2017-08-09 80 views
0

我试图做一个核心请求,应该返回一堆JSON数据,但由于某种原因请求失败。我迄今所做的代码如下:在jQuery CORS GET请求

$(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. 

注:相同的URL与授权参数在邮差工作正常。看不出有什么问题。

回答

0

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

+0

我在vhost.conf中添加了'Header set Access-Control-Allow-Origin'*“'根据这个帖子:[link](https://enable-cors.org/server_apache.html)。之后,我运行了命令'ln -sf /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load'.Still我得到的回应是一样的。 –

+0

我调试了一下,我发现这个:'Www-Authenticate:Bearer realm =“api”'。这是从哪里来的?它应该不是测试? –