向另一个域发送和接收数据

时间:2012-04-27 14:56:00

标签: javascript ajax json xmlhttprequest jsonp

我正在尝试编写插件。我不能使用任何库或框架。 在任何网站(域)我想从我自己的域开始编写脚本。

例如: 在域名A 下的网站代码中,我从域B

开始编写代码
<script src="http://domain-b.com/myscript.js" type="text/javascript"></script>

JavaScript代码(myscript.js)

type = 'GET';
url = 'http://domain-b.com/echojson.php';
data = ‘var1=1&var2=2’;

_http = new XMLHttpRequest();

_http.open(type, url + '?callback=jsonp123' + '&' + data, true);
_http.onreadystatechange = function() {
      alert(‘Get data: ’ + _http.responseText);
}
_http.send(null);
来自 http://domain-b.com/echojson.php

脚本总是给出答案:

jsonp123({answer:”answer string”});

但是在JavaScript控制台中,我看到一个错误(200),而AJAX没有得到任何东西。

2 个答案:

答案 0 :(得分:0)

像LAB,yep / nope或Frame.js这样的脚本加载器旨在绕过同源策略。他们加载脚本文件,因此请求的文件必须如下所示:

response = {answer:”answer string”};

答案 1 :(得分:0)

如果您使用的代码就像您在此处发布的那样,那么它不起作用,因为您正在使用data变量的瑕疵!

相关问题