简体   繁体   English

在另一个域之间发送和接收数据

[英]Send and receive data to and from another domain

I'm trying to write a plugin. 我正在尝试编写一个插件。 I can not use any libraries or frameworks. 我不能使用任何库或框架。 At any website (domain) I would like to start a script from my own domain. 在任何网站(域)中,我都想从自己的域中启动脚本。

For example: In the code of the website under domain A I put a code starting the script from domain B 例如:在域A下的网站代码中,我放置了一个从域B开始脚本的代码。

<script src="http://domain-b.com/myscript.js" type="text/javascript"></script>

The code of JavaScript (myscript.js) 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);

Script from http://domain-b.com/echojson.php always gives the answer: 来自http://domain-b.com/echojson.php的脚本总能给出答案:

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

But in a JavaScript console I see an error (200) and AJAX doesn't get anything. 但是在JavaScript控制台中,我看到一个错误(200),而AJAX没有得到任何结果。

Script loaders like LAB, yep/nope or Frame.js were designed to get around the same-origin policy. 诸如LAB, yep / nopeFrame.js之类的脚本加载器旨在绕开同源策略。 They load a script file, so the requested file would have to look like: 他们加载脚本文件,因此请求的文件必须看起来像:

response = {answer:”answer string”};

如果您像在此处发布代码那样使用代码,则该代码不起作用,因为您正在为data变量使用撇号!

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

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