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

asked Mar 5, 2013 at 16:20
2
  • 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. Commented Mar 5, 2013 at 16:44
  • You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. Commented Mar 5, 2013 at 16:47

1 Answer 1

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

answered Mar 5, 2013 at 16:46
Sign up to request clarification or add additional context in comments.

7 Comments

Didn't seem to help, but i'll run though a few tests and place them here:
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
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 []?
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);
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.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.