doukeng7426 2013-03-05 16:20
浏览 31
已采纳

ajax Json返回null

I have a simple ajax call to php with a json response. I have written these multiple times, but have never come across this issue.

The Ajax sends over the "id" parameter fine and the PHP receives it, does it's work and sends back the json response, which is where the issue begins. one of the parameters is always null, for a reason i can't seem to find. I have tested the php manually, and it returns both the values. i have checked the ajax to see if it revives the id parameter, it does. So the issue is some where around the json response being sent, and it being received by the jquery ajax.

 // This gets the paramaters from the url
 theParams = parseURLParams(document.URL);

 // ^^ it returns an id, like this  {"id":"4a17bcb93fe3fac3978671a66959d902"}

 $.ajax({
    url: 'viewer_code.php',
    type: 'GET',
    dataType: 'json',
    data: {id: theParams.id},
    success: function(dataImg) { 

    alert(dataImg.imgUrl);

    }
});

and the PHP (All seems fine & all will be sanitized)

    $id = $_GET['id'];

    $q = "SELECT * FROM `images` WHERE id = '$id'";
    if(!($result_set = mysql_query($q))) die(mysql_error());
        $row = mysql_fetch_array($result_set);

        $thumb = $row['thumb'];
        $image = $row['image'];

        header('Content-Type: application/json');
        echo json_encode(array("imgUrl" => $image, "id" => $id));

when the PHP is tested manually, it returns: {"imgUrl":"pictures/75de7c1c30d956113f937a8e685f7e50.jpg","id":"4a17bcb93fe3fac3978671a66959d902"}

It's the imgUrl that always returns null, anyone have any idea why this is happening? Oh and i have tried switching from GET to POST as previous questions on SO have suggested, but it didn't make any difference.

Many thanks in advance for any help, cheers guys :)

  • 写回答

1条回答 默认 最新

  • dongtan9066 2013-03-05 16:46
    关注

    Have you try instead of id: theParams.id using id: 1 I had a big problem trying to handle variables that were not correct json.

    In the other hand I am doing a similar code but do not have

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

    Have you try in your php file echo json_encode(array("imgUrl" => 'image.jpg', "id" => '1')); Depending on those tests maybe I could help you more

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题