最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - echo'd PHP encode JSON called via AJAX returns what exactly? - Stack Overflow

programmeradmin4浏览0评论

I think I'm missing something here:

Using AjAX I get some data from a database and send it back in JSON format $jsondata = array();

while ($Row = mysql_fetch_array($params))
{

    $jsondata[]= array('cat_id'=>$Row["cat_id"], 
                          'category'=>$Row["category"], 
                     'category_desc'=>$Row["category_desc"],
                     'cat_bgd_col'=>$Row["cat_bgd_col"]);
};

echo("{\"Categories\": ".json_encode($jsondata)."};");

No problem so far I think.

On the cleint side I receive back the above into

ajaxRequest.responseText

and if I do this

var categoriesObject = ajaxRequest.responseText; 
alert(categoriesObject);

I see what I expect to see ie the entire array in the alert.

Where it all goes wrong is trying to access the response. The error I get is that the "categoriesObject" is not an object - if not what is it? what's bugginh me is that I can't even access it like this:

document.write(categoriesObject.Categories[0].category);

so what am I doing wrong?

I think I'm missing something here:

Using AjAX I get some data from a database and send it back in JSON format $jsondata = array();

while ($Row = mysql_fetch_array($params))
{

    $jsondata[]= array('cat_id'=>$Row["cat_id"], 
                          'category'=>$Row["category"], 
                     'category_desc'=>$Row["category_desc"],
                     'cat_bgd_col'=>$Row["cat_bgd_col"]);
};

echo("{\"Categories\": ".json_encode($jsondata)."};");

No problem so far I think.

On the cleint side I receive back the above into

ajaxRequest.responseText

and if I do this

var categoriesObject = ajaxRequest.responseText; 
alert(categoriesObject);

I see what I expect to see ie the entire array in the alert.

Where it all goes wrong is trying to access the response. The error I get is that the "categoriesObject" is not an object - if not what is it? what's bugginh me is that I can't even access it like this:

document.write(categoriesObject.Categories[0].category);

so what am I doing wrong?

Share Improve this question asked Jan 30, 2011 at 22:41 T9bT9b 3,5025 gold badges35 silver badges51 bronze badges 1
  • 1 You need to parse the string into an object. Either using eval() which is problematic, or using a JSON parser. See here: json/js.html Side note, frameworks like jQuery have this built in – Pekka Commented Jan 30, 2011 at 22:45
Add a ment  | 

2 Answers 2

Reset to default 11
  1. You should not create JSON manually. Use:

    echo json_encode(array('Categories' => $jsondata));
    

    or just

    echo json_encode($jsondata);
    

    I don't see a reason to add Categories.

  2. You have to decode the JSON on the client side, using JSON.parse (available in most browsers, but also available as script):

    var data = JSON.parse(ajaxRequest.responseText);
    
  3. If you want to be very correct, add

    header('Content-type: application/json');
    

    to your PHP script.

Are you acutally parsing the JSON? It won't work without.

var categoriesObject = JSON.parse(ajaxRequest.responseText);
发布评论

评论列表(0)

  1. 暂无评论