2016-11-07 117 views
-1
var url= 'http://sample.net:8051/admin/singleprofile'; 
     $.ajax({ 
      url: url, 
      crossOrigin: true, 
      type: 'GET', 
      xhrFields: { withCredentials: true }, 
      accept: 'application/json' 
     }).done(function (data) { 
      console.log(data);     
     }).fail(function (xhr, textStatus, error) { 
      var title, message; 
      switch (xhr.status) { 
       case 403: 
        title = xhr.responseJSON.errorSummary; 
        console.log('Please login to your server before running the test.'); 
        break; 
       default: 
        title = 'Invalid URL or Cross-Origin Request Blocked'; 
        console.log('You must explicitly add this site (' + window.location.origin + ') to the list of allowed websites in your server.'); 
        break; 
      } 
     }); 

从上面的代码中我得到以下问题: 必须明确这个网站(' + window.location.origin + ')添加到列表允许您的服务器上的网站。跨来源请求阻止:同源策略不允许读取远程资源访问http://sample.ent

+0

您需要完全按照错误说明的内容进行操作:将您的起源添加到允许的CORS列表中,以便从服务器发送标头'Access-Control-Allow-Origin:http:// sample.net'。 –

回答

0

所有现代浏览器都遵循CORS(跨源资源共享)策略。如果您从本地服务器向外部服务器发送请求(“http://sample.net:8051/”),则浏览器不会允许您访问资源。如果您要求数据区分服务器,或者即使您具有相同的IP但在不同的端口上运行,您将面临此问题。

有关详情,请点击以下链接:
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

但这种限制不会在那里,一旦你部署的一切在真实环境中一台服务器。
作发展用途:
为前端: 你可以安装插件CORS铬。这个插件可以启用/在浏览器

禁用CORS限制对于后端: 后端服务器可以响应与它作为*值发送“访问控制允许来源”标头。所以它会接受任何来源的资源请求。

相关问题