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

3
  • What do you mean direct execution is not possible. Did you use Dartium? Commented Jan 28, 2013 at 8:37
  • Trying to call php from dart gets some error like "access-control-allow-origin" Commented Jan 28, 2013 at 9: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. Commented Jan 28, 2013 at 9:51

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

2 Comments

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.
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.
0

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

1 Comment

That's a comment, but not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.