When the JSON value returned by Eval, firebug prompts error: Invalid label)

Source: Internet
Author: User

Http://hi.baidu.com/mallor/blog/item/22a4dc1f9497a465f724e4e3.html

We often return a value in JSON format on the server side, which can be directly used as a complete object in the script. However, many new users usually encounter an error message: Invalid label, this problem is often distressing, because the correct JSON format is prompted with an error.

Assume that you return a JSON string from the server:

Javascript:

 
 
  1. {
  2. siteName : "CodeBit.cn",
  3. siteUrl : "http://www.CodeBit.cn"
  4. }

Run the following command in eval mode in the script:

Javascript:

 
 
  1. var result = eval(o.responseText);
  2. alert(result.siteName);

The following message is displayed:Error: Invalid label

In fact, our JSON format is not incorrect, but when Eval is used, you should enclose your JSON value in brackets:

Javascript:

 
 
  1. var result = eval("(" + o.responseText + ")");
  2. alert(result.siteName);

Haha, this is often the easiest to ignore, but once there is an error, it is also the most headache :)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.