Reply LinkBack Thread Tools Search this Thread
Old 13-11-2006, 18:59   #1 (permalink)
gregor
WHOA!
 
gregor's Avatar
 
Join Date: Jun 2005
Location: Toronto, Canada
Posts: 61
JSON help - eval produces syntax error

I'm trying to evaluate a JSON response but I'm getting a syntax error:

Code:
var objR = eval("(" + this.xhr.responseText + ")"); // produces 'syntax error: ()'

The response itself looks like this:

Code:
{ "user" : { "update" : "OK", "country" : "CA", "email" : "member@example.com", "firstname" : "Gregor", "lastname" : "MacGregor", "gender" : "M", "username" : "greg", "postal" : "90210", "agree" : "true" } }

The process is simple:

1. I post some form data a la Ajax to a template

2. Posted data gets processed by a proprietary server side language (the company I work
for uses their own homebrew programming language).

3. If everything goes well the new data to gets written to the template like this:

Code:
{ "user" : { "update" : "OK", "country" : "##country##", "email" : "##email##", "firstname" : "##firstname##", "lastname" : "##lastname##", "gender" : "##gender##", "username" : "##username##", "postal" : "##postal##", "agree" : "##agree##" } }

4. When the XMLHttpRequest object's status is 200 and readystate is 4 I eval() the response text (JSON data)

Despite the syntax error, if I do something like this:

Code:
var objR = eval("(" + this.xhr.responseText + ")"); var user = objR.user; alert(user.username);

... Firefox logs the syntax error BUT an alert pops up with the username value. So, syntactically, something is awry but I am actually getting back an object that I can access.

Any ideas on how to get rid of the syntax error?
  Reply With Quote
Old 14-11-2006, 03:22   #2 (permalink)
stickmus
hmmm...
 
stickmus's Avatar
 
Join Date: Jan 2004
Location: Yorkuk
Posts: 2,130
What are the quoted brackets "(" for? Have you tried it without?

Code:
var objR = eval(this.xhr.responseText);
__________________
George Smith - a vain attempt to get higher on google for my name

  Reply With Quote
Old 14-11-2006, 22:12   #3 (permalink)
gregor
WHOA!
 
gregor's Avatar
 
Join Date: Jun 2005
Location: Toronto, Canada
Posts: 61
Quote:
Originally Posted by stickmus
What are the quoted brackets "(" for?

See here: http://www.thescripts.com/forum/thread499662.html

...the error that I was receiving seems to have magically disappeared: I have no idea why but it has. So I'll leave it at that. Thanks tho stickmus...
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8