3

There have been some post with my similar problem: How do I iterate over a JSON array using jQuery/AJAX call from PHP? but not quite the same.

I'm getting and error from jquery: a is null

It is because of the code I've added to loop through the json data:

$(function () 
{
$.ajax({ 
 url: 'ajax_dashboard/api.php', //the script to call to get data 
 data: "", 
 dataType: 'json', 
 success: function(data) 
 {
 $.each(data, function() {
 $.each(this, function(k, v) {
 $('#output').append("<b>key: </b>"+k+"<b> value: </b>"+v)
 .append("<hr />");
 });
 }); 
 } 
});
}); 

And here is the php file (which I did verify gives valid JSON format):

$query_camera_name = "SELECT camera_name, camera_status, camera_quality, email_notice, camera_hash, camera_type FROM #__cameras WHERE user_id=".$user->id." AND camera_status!='DELETED'";
$db->setQuery($query_camera_name);
//get number of cameras so we can build the table accordingly
$db->query();
$num_rows = $db->getNumRows();
// We can use array names with loadAssocList.
$result_cameras = $db->loadAssocList();
echo json_encode($result_cameras);
?>

This returns this json formatted data:

[
 {
 "camera_name": "ffgg",
 "camera_status": "DISABLED",
 "camera_quality": "MEDIUM",
 "email_notice": "DISABLED",
 "camera_hash": "0d5a57cb75608202e64b834efd6a4667a71f6dee",
 "camera_type": "WEBCAM"
 },
 {
 "camera_name": "test",
 "camera_status": "ENABLED",
 "camera_quality": "HIGH",
 "email_notice": "ENABLED",
 "camera_hash": "6ab000ef7926b4a182f0f864a0d443fc19a29fdd",
 "camera_type": "WEBCAM"
 }
]

If I remove the loops the "a is null" error is gone. What am I doing wrong?

asked Nov 30, 2011 at 16:36
3
  • 4
    jsfiddle.net/2WNWg -- seems to work here. Commented Nov 30, 2011 at 16:45
  • Is the name of your table really #__cameras? Is the query itself correct? Could some unescaped stuff have gotten in through $user->id? Commented Dec 1, 2011 at 1:13
  • Yes, that is Joomla convention for tables. The file is fine as you can see the json output generated from that php file works fine. It must be something with the jquery ajax call that doesn't make it work. Commented Dec 1, 2011 at 1:23

3 Answers 3

2

Your iteration code works just fine: http://jsfiddle.net/SuyMj/

The error is elsewhere.

Edit:

Try this to help debug.

success: function(data, textStatus, xhr) {
 console.log(xhr);
 ...
}

xhr will contain a lot of information about the request being made. What does the responseText contain? What is the statusText?

answered Nov 30, 2011 at 16:46

11 Comments

Uh oh, thx. Now I'm not sure where to look. It is odd that removing the loops removes the a is null error.
What line does the error occur on? In what .js file? What version of jQuery?
<script src="ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">. I get a is null in firebug: jquery.min.js (line 2)
Can you load up the non-minified version? (remove .min)
I should also note that this is in Joomla, so just in case I've put in jQuery.noConflict() just in case.
|
1

Your code works fine:

http://jsfiddle.net/QSvNy/

So the error is not there.

answered Nov 30, 2011 at 16:47

Comments

0

I don't see that you set the Content-Type of your response from php. Possibly the mime type of your response is incorrect and so jQuery does not parse the response as json.

Try this in your php before you echo your json:

header('Content-Type: application/json');
answered Nov 30, 2011 at 20:38

1 Comment

Thanks but that didn't help either. I did debugging with @KevinB and it must be the php code as the value is null when it gets to the jquery code. Any further insight would be appreciated.

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.