CORS with Dart, how do I get it to work?

前端 未结 2 551
栀梦
栀梦 2020-12-01 21:07

Just started tinkering with Dart and I decided to write a simple Http Server and a client. My server code:

#import         


        
相关标签:
2条回答
  • 2020-12-01 21:34

    Was facing the same problem. Below is my server code. It just prints the query parameters. Added access control headers to fix the issue.

        HttpServer.bind('127.0.0.1', 8080).then((server){
        server.listen((HttpRequest request){     
          request.uri.queryParameters.forEach((param,val){
            print(param + '-' + val);
          });
    
          request.response.headers.add("Access-Control-Allow-Origin", "*");
          request.response.headers.add("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");
    
          request.response.statusCode = HttpStatus.OK;
          request.response.write("Success!");
          request.response.close();
        });
      });
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-01 21:35

    you could simplify your life and run both server/client scripts from the same host:port address. There is a small webserver example at http://www.dartlang.org/articles/io/#web-servers that serves static files too. Add your '/api' handler and make sure your client files are in the same directory. The example server is a lot slower than the Dart Editor builtin server that runs on port 3030.

    0 讨论(0)
提交回复
热议问题