2

I established a RESTful server, and I can get a simple string with my Chrome or IE using this URL: "http://localhost:8080/WebService/RESTful/getString"

But when using Dart, it always returns a error message:

"[object XMLHttpRequestProgressEvent]" 

From the onError() callback method, I'm sure that server returns a string with "text/plain" MIME type in Java.

Here is the code:

import 'dart:html';

void main() {
    HtmlElement btn = document.body.querySelector("#btn");
    btn.onClick.listen(onClick);
}

void onClick(Event v) {
  var url = "http://localhost:8080/WebService/RESTful/getString";
  HttpRequest.getString(url).then((str) {
    window.alert(str.toString());
  }, onError: (e) {
  window.alert(e);
 });
}

Who can help me ?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
dylb1973
  • 55
  • 2
  • 2
    You're alerting the `XMLHttpRequestProgressEvent` which contains the actual error. Inspect `e` in your browser's debugger to see the real problem. – Simon MᶜKenzie Apr 02 '15 at 02:04
  • how to inspect e in my browser's ? sorry ,I'm a beginner in dart . – dylb1973 Apr 02 '15 at 02:06
  • 1
    Most browsers have a debug console - Firefox (Ctrl-Shift-S), IE (F12), Chrome (Ctrl-Shift-I). You can then place a breakpoint inside your `onError` function and inspect all variables which are in scope. – Simon MᶜKenzie Apr 02 '15 at 02:10
  • Thanks ,I get the Error information XMLHttpRequest cannot load http://localhost:8080/WebService/RESTful/getString. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. – dylb1973 Apr 02 '15 at 02:18

1 Answers1

1

If you try to fetch resources from another server than the one your page was loaded from, this server needs to return CORS headers otherwise your browser refuses to fetch form this other server. It depends on your server how this can be configured or added.

See for example
- How do you add CORS headers in Redstone interceptor?
- CORS with Dart, how do I get it to work?

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567