开发者

Jquery array returns after post

Hi I am doing one checkscript with jquery and php I have written this :

$.post("chatController.php", {
    ssha:"<?php echo $sechs1;?>", 
    user: "<?php echo $_SESSION["username"];?>", 
    class:1
 }, function(data){ 
    $.each(data, alert(this));
 }, "json");
开发者_如何转开发

And from the PHP file I get something like :

echo json_encode(array("John","2pm"));

This array can have 0,1,2....unlimitied entries.. How I alert each of this entrie one by one ?

Perhaps something like:

For each data > alert(this)


You are calling alert immediately and passing its return value to $.each. You have to pass a function reference instead:

$.each(data, function(index, item){
    alert(item);
});

Be careful with using class as property name. It is a reserved keyword and you could have problems with it in browsers. Either put it in a string:

'class': 1

or rename it.


to have proper json you must have key value pairs in your php array, something like:

json_encode(array("name"=>"John","time"=>"2pm"));

to list all object properties you can do:

for(var prop in data){
  if(data.hasOwnProperty(prop)){  // this prevents inherited properties
    console.log(prop);
  }
}


Perhaps you are searching for JQuery.each().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜