我在我的服务器上使用 dart,并使用 angulardart 作为我的客户端。我可以通过我的服务器上的 http.get 请求数据,工作正常 - 但我无法让 POST 工作。
服务器:(服务器使用“开始”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);
});
添加CorsHeaders:
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”。”
我还尝试了 Google Chrome 中的高级休息插件来向该 URL 发出 POST 请求。这些请求确实有效。我是否错过了 angulardart POST 请求中的某些内容?服务器和客户端运行在不同的端口上。