problems with dart-server/angulardart and CORS

前端 未结 1 1120
清酒与你
清酒与你 2021-01-15 00:23

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.

Server

相关标签:
1条回答
  • 2021-01-15 00:38

    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();
     }
    
    0 讨论(0)
提交回复
热议问题