How to make Request.JSON work in cross-domain mode (Mootools)?

I am executing a file index.htmlfrom test1.com server. The Mootools library file is included in this index.htmlfile.

The following is a script that calls a PHP page:

<script>
  var request = new Request.JSON({
    url: 'http://test1.com/ajaxtest.php',
    onSuccess: function(data) {
      // the request was completed.
      alert(JSON.stringify(data));
    }
  }).send();
</script>

ajaxtest.php

<?php
  $arr['age'] = 30;
  $arr['place'] = 'London';
  echo json_encode($arr); exit;
?>

By performing index.html, I get the correct conclusion

{"age": 30, "place": "London"}

Now ajaxtest.phplocated on another server, say test2.com. How to change the above script to make it work as before?

+4
source share
1 answer

Not sure if this will be useful for you now.

To request a cross site, you need to use the Request.JSONP Class object:

new Request.JSONP({
url: "http://search.twitter.com/search.json",
data: {
    q: "Arsenal"
},
onComplete: function(tweets) {
    // Log the result to console for inspection
    console.info("Twitter returned: ",tweets);
}
}).send(); 
+2

All Articles