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

前端 未结 2 568
栀梦
栀梦 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.

提交回复
热议问题