跨域Json解析使用JQuery错误在HTML中显示 [英] Cross domain Json parse to display in HTML using JQuery error
问题描述
我做了一些研究如何解析json到html与jsonp,因为json在远程域。但我不知道为什么是这个错误和如何处理它:
I did some research on how to parse Json into html with jsonp,because the json is on remote domain. But I can't figure out why is this error and how to handle it:
Uncaught SyntaxError: Unexpected token :
这是我的代码:
$("document").ready(function() {
var url = "http://demos.158.bg/football/apis/eplApiV1.php?action=getRoundListByLeagueID&leagueID=7";
$.getJSON(url + "?callback=?", null, function(jsonp) {
$("#div-my-table").text("<table>");
$.each(data, function(i, item) {
// doing some stuff here
});
$("#div-my-table").append("</table>");
});
});
感谢您的建议。
找到溶液。对于那些谁会来到这个职位只需在您的域上的php文件:
Found solution. For those who will come to this post just make php file on your domain with:
<?php
echo file_get_contents($remote_domain_url);
?>
文件url在getJSON。您将不再需要JSONP。
已编辑的代码:
and include that php file url in getJSON. You will no longer need JSONP. Edited code:
$(document)。ready(function(){
var url = http://your.domain/phpfile.php ;
$("document").ready(function() { var url = "http://your.domain/phpfile.php";
$.getJSON(url, null, function(data) {
$("#div-my-table").text("<table>");
$.each(data, function(i, item) {
// doing some stuff here
});
$("#div-my-table").append("</table>");
});
});
推荐答案
您尝试提出JSONP请求,服务器正在使用JSON进行响应。您必须修改服务器端代码( demos.158.bg
)以支持JSONP(即查找回调
参数,然后返回一个由该值组成的应用程序/ javascript
文档,然后(
数据,然后)
。
You are trying to make a JSONP request, but the server is responding with JSON. You would have to modify the server side code (on demos.158.bg
) to support JSONP (i.e. look for a callback
argument in the query string and then return an application/javascript
document consisting of that value, then (
, then the data, then )
.
这篇关于跨域Json解析使用JQuery错误在HTML中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!