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

十年热恋 提交于 2019-11-27 09:15:29

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.

Sarvesh

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.

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