1

I am using JQuery to make an ajax request. In my php, I have an associative array. Here is the main portion of it:

$log = array('data','moredata');
$eventine = array('this','format');
$logID = array('another','array');
$data['log'] = $log;
$data['time'] = $eventtime;
$data['id'] = $logID;
echo json_encode($data);

When I return the json object. I use this portion of javascript to get the values:

//after getting data back from ajax call
x = JSON.parse(data);
       for (i = 0; i < x.log.length; i++) {
    results += x.time[i] +": "+x.log[i];
    results += "<br />";
                    }

The code works perfectly. My complaint is that it is throwing an error in the browser's firebug and IE log. It says I have a JSON.parse error. I've looked everywhere online and couldn't find anything similar.

The strange thing is.This code is working. But it keeps saying there is an error.

If anybody could assist me and telling me what I've done wrong, I would be very grateful.If you need more information, I am happy to post more code. Never posted here before and not sure on limits.

2
  • 2
    Can you show the JSON? What line are you getting the parse error exactly? Commented Mar 17, 2011 at 14:18
  • Hi, it says in browser json.Parse then has an error symbol next to it. I am getting the error on the bit that says x = JSON.parse(data)...thanks Commented Mar 17, 2011 at 14:39

1 Answer 1

1

Given your code you have a typo: $eventine = array('this','format'); but $data['time'] = $eventtime; maybe your problem is that x.time[i]in your js is invalid, since x.time is not an array like you pressume ?

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your answer. Actually, the code I have is correct. I just typed this out instead of cutting and pasting and made a mistake. I've heard associative arrays are not permitted there, however, my code does work as I expect it. I just get ugly errors in the browsers
Hi. I just figured it out after hours of debugging. Basically, I was grabbing information dynamically (from a database). Then I was encoding it back into javascript using json. Sometimes the resultset was empty, and I was actually effectively running JSON.parse() which is invalid. As for it working, that's because I was running it in a loop and was getting the results out of ones that worked.

Your Answer

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.