Looping through jsonp with jquery
This question already has an answer here:
我需要的循环看起来像这样
function jsonpCallback(data) {
$.each(data, function (i, item) {
$('#jsonpResult').append(
$('<div/>')
.addClass("myclass")
.text(item.timestamp) //item.timestamp
);
});
}
Its because your jsonpresponse is one array, and you are trying to use it like a single object.
function jsonpCallback(data){
$('#jsonpResult').text(data.timestamp); // Add the [0] and look what happens.
}
The correct way to use the response is:
function jsonpCallback(data){
$('#jsonpResult').text(data[0].timestamp);
}
So if u need to iterate will be something like this
Using Each:
function jsconpCallback(data){
data.each(function(item){
$('#jsonpResult').append(item.timestamp+"<br />");
});
}
or standard for:
function jsonpCallback(data){
for(var i = 0 ; i < data.length ; i++){
$('#jsonpResult').append(item.timestamp+"<br />");
}
}
Im using append because we are expecting multiple data (one array).
In the case you need only one big object, remove de [] on the PHP file where you are sending de jsonP response.
Hope it helps.
链接地址: http://www.djcxy.com/p/24534.html上一篇: 在Array中使用内部构造函数的Javascript
下一篇: 使用jquery循环浏览jsonp