AJAX方式发送远程请求报错:No 'Access-Control-Allow-Origin' header

2015-10-21 19:08:24   来源:   评论: 点击:

AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http: Response to preflight request doesn t pass access control check: No Access-Control-Allow-Origin header is present on
AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http://www.shikezhi.com/ajax/getData Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.

 

解决办法:使用jsonp格式, ajax请求参数dataType:'JSONP',如:

$.ajax({
   method: "GET",
   url: "http://www.shikezhi.com/ajax/getData",
   dataType: 'JSONP',//加入这个选项即可
   data:params,
   jsonpCallback: "jsonpcallback",
   beforeSend: function (xhr) {
   },
   success: function (response) {
  console.log(response);
   },
   error: function (jqXHR, textStatus, errorThrown) {
   }
});

相关热词搜索:ajax jsonp

上一篇:Facebook 是如何构建第一个跨平台的 React Native APP 下一篇:JavaScript中数组的合并以及排序实现示例
分享到: 收藏