dart-server/angulardart 和 CORS 的问题 [英] problems with dart-server/angulardart and CORS
问题描述
我在我的服务器上使用 dart,而 angulardart 作为我的客户端.我可以通过服务器上的 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");}
这是我的代码.正如我已经说过的,从 angular 到我的服务器的 http.get 确实有效.通过 angular http-service 失败发布到我的服务器:请求的资源上不存在 'Access-Control-Allow-Origin' 标头.因此,不允许访问 Origin '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 中的高级 rest 插件来向该 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屋!