简体   繁体   中英

JSON return null data

My php version is 5.2.17 and when i do json_decode($_GET['getItem']) return null result!!

function getItem(id){
$.getJSON(
    "items.php", 
    { getItem: '{"item": "' + id + '"}'},   
    function(json) {
        $("#"+json.itemName).html(json.itemData);
        }
);

items.php

header('Content-Type: application/json');
$jItemArray = json_decode($_GET['getItem']);
var_dump($jItemArray);
die;

I think the params are going to get URI encoded. I would output the value of $_GET['getItem'] to see what it is. Im guessing you just need to run it through urldecode first, before running json_decode on it.

With that said... is there a reason you to send json instead of standard parameter encoding?

Yeah the problem was in $_GET['getItem'] i found it's data wasn't correct like that
array(1) { ["getItem"]=> string(18) "{\\"item\\": \\"35\\"}" }
and by turning of magic quotes in php.ini it works perfectly

magic_quotes_gpc = Off
Thank you all for pointing me out

Your php should be:

header('Content-Type: application/json');
echo json_encode($_GET['getItem']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM