Javascript JSONP打印所有数据
所以我对JSONP有一个问题。我最终设法将JSONP数据加载到html文件中,但似乎无法打印所有数据。我已经尝试了for循环和$。每个,但没有成功 php文件中的JSONPJavascript JSONP打印所有数据,javascript,ajax,loops,jsonp,Javascript,Ajax,Loops,Jsonp,所以我对JSONP有一个问题。我最终设法将JSONP数据加载到html文件中,但似乎无法打印所有数据。我已经尝试了for循环和$。每个,但没有成功 php文件中的JSONP <?php echo $_GET["callback"] ?> ( { "expo":"pit", "datum":"05.06.2011 - 05.06.2016", "img":"images/pit_home.jpg", "link":"indexpit.html" }, { "expo":"Space O
<?php echo $_GET["callback"] ?> (
{
"expo":"pit",
"datum":"05.06.2011 - 05.06.2016",
"img":"images/pit_home.jpg",
"link":"indexpit.html"
},
{
"expo":"Space Odessy 2.0",
"datum":"17.02 - 19.05.2013",
"img":"images/so_home.jpg",
"link":"indexso.html"
}
);
(
{
“世博会”:“坑”,
“基准”:“2011年6月5日-2016年6月5日”,
“img”:“images/pit_home.jpg”,
“链接”:“indexpit.html”
},
{
“世博会”:“太空颂歌2.0”,
“基准”:“2013年2月17日至5月19日”,
“img”:“images/so_home.jpg”,
“链接”:“indexso.html”
}
);
用于调用JSONP的脚本
<script type="text/javascript">
$.ajax({
type: 'GET',
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
dataType: 'jsonp',
url: 'http://mllsdemode.be/Ex-cache/home.php',
success: function(json) {
for (var key in json) {
var el = document.getElementById("home");
el.innerHTML = "<li><a href=" + json.link + " data-ajax='false'><img src=" + json.img + "><div class='dsc'>" + json.expo + "<br><em>" + json.datum + "</em></div></a></li>";
}
},
error: function() { alert("Error reading jsonP file"); }
});
</script>
$.ajax({
键入:“GET”,
JSONPCCallback:'jsonCallback',
contentType:'应用程序/json',
数据类型:“jsonp”,
网址:'http://mllsdemode.be/Ex-cache/home.php',
成功:函数(json){
for(json中的var键){
var el=document.getElementById(“主”);
el.innerHTML=“”;
}
},
错误:函数(){alert(“读取jsonP文件时出错”);}
});
有人知道我应该怎么打印所有信息吗?目前,我只获得pit的数据,而不是空间ODESY 2.0的数据。([
The JSONP should be:
<?php echo $_GET["callback"] ?> (
[
{
"expo":"pit",
"datum":"05.06.2011 - 05.06.2016",
"img":"images/pit_home.jpg",
"link":"indexpit.html"
},
{
"expo":"Space Odessy 2.0",
"datum":"17.02 - 19.05.2013",
"img":"images/so_home.jpg",
"link":"indexso.html"
}
]
);
{
“世博会”:“坑”,
“基准”:“2011年6月5日-2016年6月5日”,
“img”:“images/pit_home.jpg”,
“链接”:“indexpit.html”
},
{
“世博会”:“太空颂歌2.0”,
“基准”:“2013年2月17日至5月19日”,
“img”:“images/so_home.jpg”,
“链接”:“indexso.html”
}]);
$(json).每个(函数(索引,项){
var el=document.getElementById(“主”);
el.innerHTML+=(“”);
});
试试这个
json = $.parseJSON(json);
现在,您可以按如下方式循环:
for(i=0; i<json.length; i++)
{
alert(json[i].expo);
}
for(i=0;iI)尝试了这个,但它没有打印任何数据。非常奇怪!我也尝试了这个解决方案,但也没有得到任何结果。它没有打印任何数据。
$(json).each(function (index, item) {
var el = document.getElementById("home");
el.innerHTML += ("<li><a href=" + item.link + " data-ajax='false'><img src=" + item.img + "><div class='dsc'>" + item.expo + "<br><em>" + item.datum + "</em></div></a></li>");
});
json = $.parseJSON(json);
for(i=0; i<json.length; i++)
{
alert(json[i].expo);
}