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

时间:2021-07-09 23:02:21
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) {
   }
});