Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

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

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

#import("dart:io");

final HOST = "127.0.0.1";
final PORT = 8080;
final LOG_REQUESTS = true;

void main() {
  HttpServer server = new HttpServer();
  server.addRequestHandler((HttpRequest request) => true, requestReceivedHandler);
  server.listen(HOST, PORT);
  print("Server is running on ${PORT}."); 
}

void requestReceivedHandler(HttpRequest request, HttpResponse response) {
  var pathname = request.uri;
  var apiresponse="";
  if (LOG_REQUESTS) {
    print("Request: ${request.method} ${pathname}");
  }
  if(pathname == '/api'){
    response.headers.set(HttpHeaders.CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers.add("Access-Control-Allow-Methods", "POST, OPTIONS, GET");
    response.headers.add("Access-Control-Allow-Origin", "*");
    response.headers.add('Access-Control-Allow-Headers', '*');
    print('welcome to the good life');
    response.outputStream.writeString("API Call");
    response.outputStream.close();
  }
}

My client code:

#import('dart:html');
#import('dart:json');

class dartjson {

  dartjson() {
  }

  void run() {
    write("Hello World!");
  }




  void fetchFeed(){
    XMLHttpRequest xhr = new XMLHttpRequest();
    var url = "http://127.0.0.1:8080/api";
    xhr.open("GET", url, true);
    xhr.setRequestHeader('Content-Type', 'text/plain');
    //xhr.setRequestHeader('Access-Control-Request-Headers', 'http://127.0.0.1:3030');
    xhr.send();
    print(xhr.responseText);
    document.query('#status').innerHTML = xhr.responseText;

  }



void main() {
  new dartjson().fetchFeed();
}

I keep getting the error:

XMLHttpRequest cannot load http://127.0.0.1:8080/api. Origin
http://127.0.0.1:3030 is not allowed by Access-Control-Allow-Origin.

What I'm I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...