CORS with Dart, how do I get it to work?
Solution 1:
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.
Solution 2:
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.
Solution 3:
here is my solution:
request.response.headers.add("Access-Control-Allow-Origin", "*");
request.response.headers.add("Access-Control-Allow-Headers", "*");
request.response.headers.add("Access-Control-Allow-Methods", "POST,GET,DELETE,PUT,OPTIONS");