简体   繁体   English

如何使用跨域json数据?

[英]How to use cross-domain json data?

I wish to access this json list: http://www.westwing.de/campaign/top/?limit=5&bp=1&json=1 and turn it into usable html on my page. 我希望访问此json列表: http : //www.westwing.de/campaign/top/? limit=5&bp=1&json =1并将其转换为我页面上的可用html。 There is one issue, the server my site runs on is a test server and does not have the same domain name (westwing.de). 有一个问题,我的站点运行的服务器是测试服务器,并且没有相同的域名(westwing.de)。

I read that you could insert it as a script tag in your document's head and use JSONP but when I do that and open the script in chrome it returns the website's homepage, not the json list. 我读到您可以将其作为脚本标签插入文档的头部并使用JSONP,但是当我这样做并以chrome打开脚本时,它将返回网站的首页,而不是json列表。

I don't have any access to the server so can't change settings etc. 我无权访问服务器,因此无法更改设置等。

Could someone point me to a good example of this or explain it to me? 有人可以给我指出一个很好的例子或向我解释吗?

  1. You cannot do x-domain JSON, unless the server supports CORS (which this one doesn't). 您无法执行x域JSON,除非服务器支持CORS(而该服务器不支持)。

  2. You can do x-domain JSONP, but that requires the coooperation of the server (which you don't have), as the server needs to return a JSONP response, rather than plain JSON. 可以执行x域JSONP,但这需要服务器的协作(您没有),因为服务器需要返回JSONP响应,而不是简单的JSON。 See Can anyone explain what JSONP is, in layman terms? 参见有人可以用外行人的术语解释什么是JSONP吗? .

  3. If the test server is on *.westwing.de , you can set document.domain = "westwing.de" on your client, which will allow you to make JSON requests as normal (as the pages will then be on the same domain). 如果测试服务器位于*.westwing.de ,则可以在客户端上设置document.domain = "westwing.de" ,这将使您能够像往常一样发出JSON请求(因为这些页面随后将位于同一域中) 。

  4. If the above doesn't apply, your best option is to write your own page on your test server, which acts as a proxy between your test domain and westwing.de (as your server side code won't be restricted by the SOP ). 如果上述方法不适用,最好的选择是在测试服务器上编写自己的页面,该页面充当测试域和westwing.de之间的代理(因为服务器端代码不受SOP的限制) 。

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

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