problems with dart-server/angulardart and CORS

落爺英雄遲暮 提交于 2019-12-01 09:29:59

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

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.

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

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();
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!