这个问题在这里已有答案:
JSON和JSONP有什么区别? 7个答案我已经在服务器中有一个.json对象。 它是正确的,没有语法错误(有效的json)。 我想通过JSONP调用此对象,因为它驻留在与我的应用程序不同的服务器中。
我想我理解如何在客户端实现它,但我不知道该怎么做与服务器部分有关。 在关注网上已有的信息时,我一直有错误。 有帮助吗?
This question already has an answer here:
What are the differences between JSON and JSONP? 8 answersI already have a .json object in a server. It is correct and has no syntax errors (valid json). I want to call this object through JSONP because it resides in a server different from my app's.
I think I understand how to achieve it client-wise, but I have no idea what to do in relation to the server part. I am having errors all the time when following the info already on the web. Any help?
最满意答案
JSONP基本上是一个“hack”,允许站点加载数据并忽略同源策略 。 它的工作原理是在页面上附加<script>标签。
事实上的方法是在您的请求中发送回调。 然后服务器将获取该名称并生成一个JS文件,该文件使用数据调用该函数。
使用jQuery,您只需附加?callback=?即可进行JSONP调用?callback=? 在执行$.getJSON时访问您的URL。
例:
$.getJSON('http://YourSite.com/path/to/json.php?callback=?', function(data){ console.log(data); // this will be parsed for you });或者,您可以使用完整的$.ajax方法:
$.ajax({ url: 'http://YourSite.com/path/to/json.php', dataType: 'jsonp', // this tells jQuery to add "callback=?" for you success: function(data){ console.log(data); // this will be parsed for you } });而不是mackning AJAX调用,jQuery实际上将附加:
<script src="http://YourSite.com/path/to/json.php?callback=jQuery12345"></script>到你的页面(注意: jQuery12345将随机生成)。
然后在您的服务器上,您需要使用有效的JavaScript文件进行响应。 它应该包含对查询字符串中传递的callback函数的调用。 像这样的东西:
jQuery12345({your: ['json', 'data']});PHP中的一个示例(适应您使用的任何服务器端语言)可以是:
$array = ['a' => 1, 'b' => 2,'c' => 3]; $callback = $_GET['callback']; header('Content-type: text/javascript'); echo "{$callback}(".json_encode($array).");";这里的所有都是它的。 有关更多信息,请参阅JSONP上的Wikipedia页面: http : //en.wikipedia.org/wiki/JSONP
JSONP is basically a "hack" to allow sites to load data and ignore the same-origin policy. It works by appending a <script> tag to your page.
The de facto way is to send a callback in your request. The server would then take that name and generate a JS file that calls that function with the data.
Using jQuery, you can make a JSONP call by simply appending ?callback=? to your URL when doing $.getJSON.
Example:
$.getJSON('http://YourSite.com/path/to/json.php?callback=?', function(data){ console.log(data); // this will be parsed for you });Or, you can use the full $.ajax method:
$.ajax({ url: 'http://YourSite.com/path/to/json.php', dataType: 'jsonp', // this tells jQuery to add "callback=?" for you success: function(data){ console.log(data); // this will be parsed for you } });Instead of makning an AJAX call, jQuery will actually append:
<script src="http://YourSite.com/path/to/json.php?callback=jQuery12345"></script>to your page (note: jQuery12345 will be randomly generated).
Then on your server, you need to respond with a valid JavaScript file. It should contain a call to the callback function passed in the query string. Something like this:
jQuery12345({your: ['json', 'data']});An example in PHP (adapt to whatever server-side language you use) could be:
$array = ['a' => 1, 'b' => 2,'c' => 3]; $callback = $_GET['callback']; header('Content-type: text/javascript'); echo "{$callback}(".json_encode($array).");";That's all there is to it. See the Wikipedia page on JSONP for more info: http://en.wikipedia.org/wiki/JSONP
更多推荐
发布评论