当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to return an indexed array object to a getJSON request in jquery

问题描述:

I am sending an Indexed Array to a PhP file in server side using getJSON. I guess I should better use associated array, but indexed array works fine and data are received at the server. I can process my Array at server and can store the array elements inside a file at server by referring to it's element using $results[i], but when I sending the whole array as a callback response, it sends "Array" only.

Here is the callback response I am sending to the client:

 echo $_GET['callback']. '('. "{'response' : " . "'". $results . "'}" .')';

if I say

 echo $_GET['callback']. '('. "{'response' : " . "'". $results[1] . "'}" .')';

it works and sends the element number 1 to the client. But I need to send the whole array.

Thanks for helping in advance.If code is needed, pls let me update.

saeid

网友答案:

insted of creating json object yourself rinventing the wheel)...use echo json_encode buili in function

echo json_encode(array('response'=> $results))

JQUERY

$.getJSON(url,function(data){
     console.log(data.response); //check your browser console..
})
分享给朋友:
您可能感兴趣的文章:
随机阅读: