javascript 我如何使用外部 JSON...?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8344609/
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
How do I use external JSON...?
提问by Derek Ho
spent a few hours trying to figure this out, but cannot for the life of me figure out what's going wrong.
花了几个小时试图弄清楚这一点,但我一生都无法弄清楚出了什么问题。
All I'm trying to do is load this:
我要做的就是加载这个:
https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json
which I believe is json, into either javascript/jquery or php and use the data.
我相信这是 json,进入 javascript/jquery 或 php 并使用数据。
I've looked into jsonp, followed some tutorials, used some demos as templates and just can't get the above data to work.
我研究了 jsonp,遵循了一些教程,使用了一些演示作为模板,但无法使上述数据正常工作。
If anyone can shed some light it would be much appreciated. It really shouldn't be this complicated, but I don't know what's going wrong.
如果有人能透露一些信息,将不胜感激。真的不应该这么复杂,但我不知道出了什么问题。
回答by Rocket Hazmat
Yep, that's JSON. The site may not support JSONP, so you're gonna have to use PHP to do this.
是的,那是 JSON。该站点可能不支持 JSONP,因此您将不得不使用 PHP 来执行此操作。
This is untested, but should work.
这是未经测试的,但应该可以工作。
<?php
$url = 'https://recruit.zoho.com/ats/EmbedResult.hr?jodigest=2cV.Sr2As6VxhLMxQGuTNij*g.Fb3J7ysduDs.AC9sU-&atslocale=en_GB&rawdata=json';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
回答by Matt
JSONP relies on the server to return a JSONP formatted response. Basically, to use JSONP the server needs to return a JSON string wrapped in a function invocation ({"foo":1}
becomes func({"foo":1})
).
JSONP 依赖于服务器返回 JSONP 格式的响应。基本上,要使用 JSONP,服务器需要返回一个封装在函数调用中的 JSON 字符串({"foo":1}
become func({"foo":1})
)。
As the server your using doesn't return a JSONP response, you cannotuse JSONP, you can only use JSON.
由于您使用的服务器不返回 JSONP 响应,因此您不能使用 JSONP,只能使用 JSON。
This is a shame, as JSON cannot be used x-domain due to the same origin policy (SOP). Therefore, the only option you have is to use a proxy server, which retrieves the JSON from the server, and either gives it to you in JSONP (see Yahoo Pipes), or which is on the same domain as the requested page (write a simple PHP script to get the file using file_get_contents()
and then echo
the output), in which case it can return the JSON.
这是一种耻辱,因为由于同源策略 (SOP),JSON 不能用于 x-domain 。因此,您唯一的选择是使用代理服务器,该服务器从服务器检索 JSON,并以 JSONP 形式将其提供给您(请参阅Yahoo Pipes),或者与请求的页面位于同一域中(编写一个使用简单的 PHP 脚本来获取文件file_get_contents()
然后echo
输出),在这种情况下它可以返回 JSON。
回答by Alex Trusler
I breifly looked at the requirementsand it looks like you need an API key as well as an account. I saw that the site provides services for XML and JSON only. It looks to be fairly well documented.
我简要地查看了要求,看起来您需要一个 API 密钥和一个帐户。我看到该站点仅提供 XML 和 JSON 服务。它看起来相当有据可查。