dart-server / angulardart和CORS的问题 [英] problems with dart-server/angulardart and CORS
问题描述
我在我的服务器和angulardart上使用dart作为我的客户端。我可以通过我的服务器上的http.get请求数据,这是工作正常 - 但我不能得到POST工作。
I am using dart on my server and angulardart as my client. I can request data via http.get on my server, that is working fine - but I can't get POST to work.
服务器:(服务器使用开始 https://github.com/lvivski/start )
Server: (the server makes use of "Start" https://github.com/lvivski/start)
//runs at 127.0.0.1:4040:
server.post("/rest/qr").listen((request) {
addCorsHeaders(request.response);
request.payload().then((map) {
print(map);
}).then((_) {
request.response.send("ok");
});
});
客户(角色):
// runs at http://127.0.0.1:3030
final String _codesUrl = "http://127.0.0.1:4040/rest/qr";
_http.post(_codesUrl, JSON.encode(temp.toMap())).then((HttpResponse response) {
print(response.status);
});
addCorsHeaders:
addCorsHeaders:
void addCorsHeaders(Response res) {
res.header("Access-Control-Allow-Origin", "*, ");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");}
这是我的代码。正如我已经说,http.get从角到我的服务器工作。通过角度http服务发送到我的服务器失败:没有访问控制允许原始标题存在于请求的资源。因此,'127.0.0.1:3030'不允许访问。
This is my code. As I already said, http.get from angular to my server does work. Post to my server via the angular http-service fail : "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '127.0.0.1:3030' is therefore not allowed access."
我也尝试过Google Chrome中的高级休息插件,对该URL发出POST请求。这些请求起作用。我错过了在angulardart POST请求的东西吗?服务器和客户端在不同的端口上运行。
I also tried the advanced rest plugin in Google Chrome to make POST-request to that URL.These requests do work. Do I miss something on the angulardart POST-requests? Server and Client run on different ports.
推荐答案
原来,一些CORS请求需要预检请求。 GET不,但POST。以下文章详细说明了所有这些:
I've run into this issue myself. Turns out that some CORS requests require a "preflight request". GET does not, but POST does. The following article explains all of this in detail:
http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server
提示:对于POST CORS请求,客户端将首先向服务器发送一个OPTIONS请求,询问CORS POST是否正常。这是我之前提到的预检请求。你需要抓住它,并回应它,然后才能得到POST。
The gist is that for a POST CORS request, the client will first send an "OPTIONS" request to the server, asking if a CORS POST is OK or not. This is the "preflight request" I mentioned previously. You need to catch it and respond back to it before you will get the POST.
我不知道如何使用Start框架回答OPTIONS请求,但是dartlang网站有一个关于如何解决vanilla dart的确切问题的教程:
I'm not sure how to answer OPTIONS requests with the Start framework, but the dartlang website has a tutorial on how to solve the exact problem with vanilla dart:
https:/ /www.dartlang.org/docs/tutorials/forms/#handling-options-requests
处理OPTION请求的代码(从上面文章):
The code to handle the OPTION requests (cribbed from the above article):
void handleOptions(HttpRequest req) {
HttpResponse res = req.response;
addCorsHeaders(res);
print('${req.method}: ${req.uri.path}');
res.statusCode = HttpStatus.NO_CONTENT;
res.close();
}
这篇关于dart-server / angulardart和CORS的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!