0

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 :)

Jamie Turner
  • 569
  • 1
  • 6
  • 14
  • Have you tried just echoing the value of the array using print_r or something instead of sending it as a json to make sure the ajax request is getting the correct values. – Pitchinnate Mar 05 '13 at 16:44
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 05 '13 at 16:47

1 Answers1

1

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

kilkadg
  • 2,157
  • 4
  • 16
  • 21
  • Didn't seem to help, but i'll run though a few tests and place them here: – Jamie Turner Mar 05 '13 at 17:08
  • test one: output: change echo json_encode(array("imgUrl" => $image, "id" => $id)); to: echo json_encode(array("imgUrl" => "image.jpg", "id" => $id)); <-- no works, so possibly no the output? running another test on input now – Jamie Turner Mar 05 '13 at 17:10
  • Just run a test on what i get back when i take away the DataType:'json'. This comes back: {"imgUrl":null,"id":["4a17bcb93fe3fac3978671a66959d902"]} <-- possibly something to do with the []? – Jamie Turner Mar 05 '13 at 17:15
  • Other option you could make to see if you are entering into the php is echoing an html. But you have to delete the dataType: 'json' in your ajax and in the alert just use alert(dataImg); – kilkadg Mar 05 '13 at 17:16
  • I've fixed it!!!! I wan't giving the "theParams.id" an index, simply changed it to "theParams.id[0] asn wallah, fixed :D. Cheers for the help mate. – Jamie Turner Mar 05 '13 at 17:19
  • Try instead of data: {id: theParams.id} this data: theParams, – kilkadg Mar 05 '13 at 17:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25616/discussion-between-kilkadg-and-user1636816) – kilkadg Mar 05 '13 at 17:22