[英]How to set up CORS in an AJAX request
I have been working on a personal webapp and have hit a little snag. 我一直在开发个人Web应用程序,遇到了一些障碍。 My API calls only work for some APIs and not for others.
我的API调用仅适用于某些API,不适用于其他API。 For the ones it doesn't work with I will usually get an error message like so.
对于那些无法正常工作的程序,我通常会收到类似这样的错误消息。
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.
After doing some research I know it is to do with CORS not being setup. 经过研究后,我知道这与未设置CORS有关。 I was wondering if it would be possible to set this up in the client when making an AJAX request.
我想知道是否可以在发出AJAX请求时在客户端进行设置。 The current way I am doing this is like so
我目前的做法是这样的
var handleRequest = function(request){
$.ajax({
type: "GET",
url: request,
success: function(data) {
var rawJSON = JSON.stringify(data, null, 2);
editor.setValue(rawJSON);
},
dataType: 'json'
});
The server you're trying to access has to grant you permission to access it. 您尝试访问的服务器必须授予您访问权限。 An IT admin has to provide you with a URL that grants you permission to hit their external server.
IT管理员必须为您提供一个URL,该URL授予您访问其外部服务器的权限。 The server you are trying to hit has to setup CORS.
您尝试访问的服务器必须设置CORS。 http://enable-cors.org/
http://enable-cors.org/
According to their docs they support JSONP. 根据他们的文档,他们支持JSONP。
https://www.meetup.com/meetup_api/ https://www.meetup.com/meetup_api/
This is your way around CORS. 这是您围绕CORS的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.