我一直在开发个人Web应用程序,遇到了一些障碍。我的API调用仅适用于某些API,不适用于其他API。对于那些无法正常工作的程序,我通常会收到类似这样的错误消息。

XMLHttpRequest cannot load https://api.meetup.com/2/cities. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.


经过研究后,我知道这与未设置CORS有关。我想知道是否可以在发出AJAX请求时在客户端进行设置。我目前的做法是这样的

var handleRequest = function(request){
  $.ajax({
  type: "GET",
  url: request,
  success: function(data) {
    var rawJSON = JSON.stringify(data, null, 2);
    editor.setValue(rawJSON);
  },
  dataType: 'json'
});

最佳答案

您尝试访问的服务器必须授予您访问权限。 IT管​​理员必须为您提供一个URL,该URL授予您访问其外部服务器的权限。您尝试访问的服务器必须设置CORS。 http://enable-cors.org/

07-24 17:56
查看更多