如何使Request.JSON跨域工作(Mootools)?

时间:2013-10-09 12:21:05

标签: javascript ajax mootools

我正在从服务器test1.com执行index.html文件。 Mootools库文件包含在此index.html文件中。

以下是调用PHP页面的脚本:

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

执行index.html时,我得到正确的输出“

  

{ “年龄”:30, “地点”: “伦敦”}

现在,ajaxtest.php驻留在另一台服务器上,比如test2.com。如何更改上面的脚本以使其像以前一样工作?

1 个答案:

答案 0 :(得分:2)

现在不确定这对你有用。

您需要使用Request.JSONP Class对象来发出跨站点请求:

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