How to debug client side dart code in Dart editor

2019-07-27 11:44发布

问题:

I have a server / client project, both written in dart. Now my server starts on port 1337 and when I run my client with the Run in dartium, my static files are served on port 3030 which allows me to debug my client code in the Dart editor.

The problem is that this causes CORS when using AJAX calls. I have properly setup my server to accept other origins (with Access-Control-Allow-Origin) but, for example, cookies aren't sent along.

Now I'm wondering: is there a way to serve my files with my server (running on 1337) and still have the possibility to debug the client side code in the dart editor?

回答1:

My understanding is that you can debug, but the real problem is that you don't get the expected data back from the server due to missing cookies.

Standard CORS requests do not send or set any cookies by default.

In order to include cookies as a part of the request, besides setting up the server, you need to specify withCredentials property, e.g.:

HttpRequest.getString(url, withCredentials:true)...

You will also need to setup server to provide Access-Control-Allow-Credentials header.

EDIT: it seems that additional issue is that you don't want to have 2 servers, each serving different part of app.

In that case, you can configure DartEditor to launch the URL, instead of files. Go to Run > Manage Launches and add create a new Dartium or Dart2JS launch with specified URL and source directory.

Another option is to select Run > Remote Connection and attach to a running instance of browser or Dart VM.

Caveat: I haven't tried these options, so I can't tell how stable they are.