TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   JSON invalid label (http://www.talkphp.com/javascript-ajax-e4x/1398-json-invalid-label.html)

Wildhoney 11-06-2007 11:34 PM

JSON invalid label
 
I feel it's important to reiterate a point I made in this article about JSON with PHP and Javascript. The Javascript error "invalid label" may be a tough one to overcome if you're blissfully unaware of why the error's being thrown.

JSON strings look something like the following:

Quote:

{"Juicy":"Grapes","Colourful":"Dragon Fruit"}
However, when feeding them in to eval, you must place parenthesis around them, so your JSON line looks like so:

PHP Code:

var szJson '({"Juicy":"Grapes","Colourful":"Dragon Fruit"})'

In an AJAX function this would look like so:

PHP Code:

var szJson = eval('(' pResponse.responseText ')'); 

After that your JSON string will be more than ready to be run through Javascript's eval without any complaints!

Salathe 11-07-2007 01:00 AM

Quote:

Originally Posted by Wildhoney
The Javascript error "invalid label" may be a tough one to overcome if you're blissfully unaware of why the error's being thrown.

So, why not explain why the error's being thrown?

Wildhoney 11-07-2007 01:09 AM

I do, it's being thrown because you've omitted the parenthesis.

Salathe 11-07-2007 01:13 AM

But why do the parenthesis solve this problem? What is the problem in the first place (besides "invalid label"). There's no point writing an article like this without explaining the problem that you're solving. It'll just leave visitors "blissfully unaware of why the error's being thrown".

Wildhoney 11-07-2007 01:36 AM

Quote:

Although some browser error messages seem obscure they often actually
provide a better clue to what is happening that it may at first appear.
In this case "invalid label" is a better clue than it appears as in
javascript/ECMAScript a label is used to identifier a point in the code
(expected to correspond with the start of a loop construct of some sort)
and consists of an Identifier followed by a colon. If an object literal
was interpreted in a context where what was intended to be a property
name in the form of a string literal was interpreted as a label (because
of the following colon) then it would be invalid and "invalid label"
would be a very direct and informative error message.

The problem here is likely that a string containing this apparent object
literal definition is begin passed directly to the - eval - function and
so is being interpreted as an entire javascript Program. The text of an
object literal definition in the wrong context can be interpreted as a
javascript Program. The surrounding braces become a Block statement, and
the contained name value pairs then look like labelled expression
statements. Under this interpretation most object literals would include
syntax errors and so fail to execute but some can happily (if
pointlessly) be executed as a javascirpt Program
Source: http://www.thescripts.com/forum/thread511372.html

Karl 11-07-2007 12:16 PM

So basically the parenthesis stops JavaScript treating the JSON like an object literal?

Wildhoney 11-07-2007 12:58 PM

That's what I gathered from it. The { } signify that it is an object literal, whereas ( ) block that interpretation.


All times are GMT. The time now is 09:45 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0