CORS - 使用 AJAX 在 Python (webapp2) Web 服务上发布 [英] CORS - Using AJAX to post on a Python (webapp2) web service
问题描述
这会很长:
好的,所以我正在开发一个 google 日历小工具,它将请求发送到托管在 Google App Engine 上的 Python webapp2 REST api.
Ok so I'm developing a google calendar gadget which sends requests to a Python webapp2 REST api hosted on Google App Engine.
当我尝试发布由于 CORS 而不允许我发布的内容时,问题就出现了.在 Chromes 的 DevTools 中,它说:
The problem comes when I try to POST something it doesn't allows me because of CORS. In Chromes' DevTools it says:
Method: OPTIONS.
Status: (failed) Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Origin https://hq34i4geprnp5vci191ljfuhcoerscl4-a-calendar-opensocial.googleusercontent.com is not allowed by Access-Control-Allow-Origin.
我知道这是因为 CORS.这里:
I'm aware that this is because of CORS. Here:
Ajax - 'Origin localhost is not由 Access-Control-Allow-Origin 允许'
它说我必须添加
Access-Control-Allow-Origin: *
对于标题,但是我又是 ajax 的新手,我想知道它是否是这样完成的:
To the headers, but then again I'm new to ajax and I wonder if it's done this way:
$.ajax({
type: "POST",
url: "https://myapp.appspot.com/service",
contentType: "application/json; charset=utf-8",
data: data,
beforeSend: function (request)
{
request.setRequestHeader("Access-Control-Allow-Origin", "*");
}
success: function(data) {
alert("AJAX done");
}
});
添加这个标题,输出不同(这让我想知道是否允许来源,虽然我真的不知道):
Adding this headers the output is different (which makes me wonder if the origin has been allowed, though I don't really know):
Method: OPTIONS.
Status: (failed) Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
XMLHttpRequest cannot load https://myapp.appspot.com/service. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers.
我什至发现了这个:
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
这让我可以做 GET 请求,但我想学习如何在没有这个的情况下做它们.
Which lets me do GET requests, but I'd like to learn how to do them without this.
也在我的网络服务器上有这个:
Also on my webserver I have this:
...
class webService(webapp2.RequestHandler):
options(self):
self.response.write('options')
post(self):
self.response.write('post')
application = webapp2.WSGIApplication([
('/', MainPage),
('/service', webService)
], debug=True)
我不知道我是否必须向网络服务器添加更多内容,也没有找到信息说我必须这样做.此外,我认为我已接近实现 CORS 请求,但是,我找不到解释这一切的示例.
I don't know if I must add something more to the webserver, nor I've found info saying that I have to. Also I think I'm near to achieve the CORS request but, I can't find THE Example that explains it all.
请帮忙.
推荐答案
好的,我修好了.
首先我意识到这里 标头是由服务器发送的,所以在 AJAX 请求中发送这些标头时我做错了.
First of all I realized here that the headers were sent by the server so I was doing wrong when sending those headers in the AJAX request.
最后,在万维网上搜索之后,我找到了我所缺少的东西.这是愚蠢的事情.我找到了解决所有问题的页面:
Finally, after searching around the worldwide web I found what I was missing. It was something stupid. I found the page that fixed it all:
http://enable-cors.org/server_appengine.html
所以最后一切看起来像这样:
So finally everything looks like this:
$.ajax({
type: "POST",
url: "https://myapp.appspot.com/service",
contentType: "application/json; charset=utf-8",
data: data,
success: function(data) {
alert("AJAX done");
}
});
在 webService 中:
And in the webService:
class webService(webapp2.RequestHandler):
def get(self):
self.response.headers.add_header('Access-Control-Allow-Origin', '*')
self.response.headers['Content-Type'] = 'application/json'
# do something
def post(self):
self.response.headers.add_header('Access-Control-Allow-Origin', '*')
self.response.headers['Content-Type'] = 'application/json'
# do something
def options(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
self.response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE'
这篇关于CORS - 使用 AJAX 在 Python (webapp2) Web 服务上发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!