Javascript 跨域 JSON 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:36:12  来源:igfitidea点击:

Cross-domain JSON request?

javascriptjsonxmlhttprequest

提问by Stefan Steiger

Question:

题:

I'm trying to use JSON accross domains, but all i find is JSON parsers, which I don't need...
I've read that it's possible to do cross-domain requests with JSON, but so far, all I see is implementations that use XMLHttpRequest...
- which means you can't use cross-domain requests, at least not outside IE 8...
I've been on http://www.json.org/, but all I find is either parsers or useless.

The best I've found with google so far is
http://devpro.it/JSON/files/JSONRequest-js.html
but this is rather a mess, doesn't work cross domain, and intra-domain neither - or rather not at all...

我试图用JSON翻过域,但所有我觉得是JSON解析器,这我不需要......
我读过,这是可以做到的跨域请求使用JSON,但到目前为止,我所看到的是使用 XMLHttpRequest 的实现......
- 这意味着你不能使用跨域请求,至少不能在 IE 8 之外......
我一直在http://www.json.org/,但我发现要么解析器要么没用。

到目前为止,我在 google 上发现的最好的是
http://devpro.it/JSON/files/JSONRequest-js.html
但这相当混乱,不能跨域工作,也不能跨域工作 - 或者更确切地说一点也不...

var the_object = {}; 
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
    if ( http_request.readyState == 4 && http_request.status == 200 ) {
            the_object = JSON.parse( http_request.responseText );
        }
};
http_request.send(null);

采纳答案by dxh

What you can do cross-domain is inject a script include:

你可以跨域做的是注入一个脚本,包括:

var s = document.createElement('script');
s.src = 'http://someotherdomain/getMeMyJs.aspx?parameter=value';
s.onload = someOptionalCallback;
s.type = 'text/javascript';

if(document.getElementsByTagName('head').length > 0)
    document.getElementsByTagName('head')[0].appendChild(s);

Now, the code returned by that request will be executed immediately. If you want for that to interact with your code, you can make sure that it's being returned with all data wrapped in a function call:

现在,该请求返回的代码将立即执行。如果您希望它与您的代码交互,您可以确保它与包装在函数调用中的所有数据一起返回:

jsonCallback({ object: json, whatever: value });

You can use that to build APIs, where you pass the name of a callback function as a request querystring parameter. Here's an example of such an API

您可以使用它来构建 API,您可以在其中将回调函数的名称作为请求查询字符串参数传递。这是此类 API 的示例

回答by Ofri Raviv

JSON is just a serialization method. There is no relation whatsoever between the method of the serialization and the question of whether or not the browser will try to stop you from accessing data across domains. (This explains why you are only finding parsers - there is nothing to JSON, except encoding and decoding it).

JSON 只是一种序列化方法。序列化的方法与浏览器是否会试图阻止您跨域访问数据的问题之间没有任何关系。(这解释了为什么您只找到解析器 - 除了编码和解码之外,JSON 没有任何内容)。

XMLHTTPRequest is just named XMLHTTPRequest. It doesn't really have anything to do with XML. It can be used to send text data, data encoded in JSON, or any others serialization method.

XMLHTTPRequest 只是命名为XMLHTTPRequest。它实际上与 XML 没有任何关系。它可用于发送文本数据、以 JSON 编码的数据或任何其他序列化方法。

There are several methods to access data cross domain. one described in David Hedlund's answer. Others can be found in answers to similar questions (see hereand here).

有多种方法可以跨域访问数据。David Hedlund 的回答中描述了这一点。其他人可以在类似问题的答案中找到(请参阅此处此处)。