繁体   English   中英

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

[英]How to make Request.JSON work cross-domain (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。 如何更改上述脚本以使其更早地工作?

不确定现在是否对您有帮助。

您需要使用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(); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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