简体   繁体   中英

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

I am executing an index.html file from the server test1.com. Mootools library file is included in this index.html file.

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;
?>

While executing index.html , I'm getting the correct output"

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

Now, ajaxtest.php is residing on another server, say test2.com. How to change the above script to make it work as earlier?

Not sure if this will be helpful to you now.

You need to use the Request.JSONP Class object to make cross site request:

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM