2

So i am trying to communicate between dart clientside and a php server side using AJAX. Since direct execution is not possible. I compiled the dart to javascript and then run it on a apache server.

json data is generated at client end, But there is no response from the the server

dart code

import 'dart:html';
import 'dart:json';

void main() {
  query("#clicker").on.click.add(callServer);

}

void callServer(Event event) {
  var data ={ 'name':"sendname"}
  ,jsondata=stringify(data);
  print(jsondata);

  var req = new HttpRequest();
  req.open('post','http://localhost:8080/darttest/server.php',true);
  //req.setRequestHeader('Content-type','application/json');
  req.send(jsondata);
  print(req.responseText);
}

php side i just echo the content received

<?php

$name = $_POST['name'];
echo $name;

?>

This is my first try at dart programming, so do let me know if this approach is even possible

de-bugged
  • 935
  • 4
  • 14
  • 34
  • What do you mean direct execution is not possible. Did you use Dartium? – Leigh Jan 28 '13 at 08:37
  • Trying to call php from dart gets some error like "access-control-allow-origin" – de-bugged Jan 28 '13 at 09:28
  • 1
    Yes, this approach is possible (and even expected). The `access-control-allow-origin` error is described in my answer below, and is a browser security issue rather than something that is Dart specific. – Chris Buckett Jan 28 '13 at 09:51

3 Answers3

2

Is localhost:8080 serving both the static Dart (as JS), and the php? If not, you're likely coming across the access-control-allow-origin issue (which is a browser security issue).

This prevents one site posting date to another site.

Work-arounds:

  1. Ensure that the site serving php returns the correct CORS headers: http://enable-cors.org/server.html
  2. Serve the static Dart/JS files from the same URL (localhost:8080)

For more information, read these:

Update Workaround 3 is described here (for Chrome / Dartium): https://groups.google.com/a/dartlang.org/d/msg/misc/kg13xtD7aXA/uxeXXrw3CG8J

You can add the parameter "--disable-web-security" to chrome.exe to disable cross domain check.

(Of course, this is only useful while you are developing)

Community
  • 1
  • 1
Chris Buckett
  • 13,738
  • 5
  • 39
  • 46
  • 1
    Hi Chris, Im currently serving JS and PHP from localhost:8080. So shouldnt my current code should work without CORS. Thnx for the links this shd help with direct dart to php . Ill give it a shot – de-bugged Jan 28 '13 at 10:10
1

To read the response, you have to put your code in a callback on readyStateChange :

var req = new HttpRequest();
req.open('post','http://localhost:8080/darttest/server.php',true);
req.on.readyStateChange.add((e){
  if (req.readyState == HttpRequest.DONE && req.status == 200){
    print(req.responseText);
  }
});
req.send(jsondata);

With your code the http request was not processed when you tried to read the response. You have to wait the completion of the request to read the response.

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
  • Do I have to separately activate a php-server or such? I use one but it feels that Chromium/Dartium is using its own webserver. The php script always returns `OK: 200` and no `request.responseText`. – poitroae Aug 08 '13 at 09:29
  • When you use _run in dartium_ the Dart Editor will launch its own server (by default on `http://127.0.0.1:3030/`). So you have to use a separated php-server. – Alexandre Ardhuin Aug 08 '13 at 09:54
0

this is not sending data between dart and php. this is sending data from dart to php!!!

Allen Wayne
  • 121
  • 6