jquery Invalid label error solution when obtaining JSON across domains _jquery

Source: Internet
Author: User
Finally, after careful silence, read the JSON official document and discover this paragraph:

JSON data is a structured data that can be easily parsed by JavaScript. If you get a data file that resides on a remote server (the domain name is different, that is, you get the data across the domain), you need to use the JSONP type. Using this type, a query string parameter is created callback=? , this parameter is appended to the URL of the request. The server side should precede the JSON data with the callback function name in order to complete a valid JSONP request. If you want to specify the parameter name of the callback function to replace the default callback, you can set the JSONP parameter of the $.ajax ().

In fact, jquery Cross-domain principle is implemented through the chain <script>, and then the callback function plus the parameters of the callback function to achieve true Cross-domain

Jquery has callback this parameter every time the request is sent across the domain, in fact the value of this parameter is the callback function name, so the server side should put this parameter to the front when sending the JSON data, the value of this parameter is often randomly generated, For example: jsonp1294734708682, you can also set the name of the callback method through the $.ajax method. After you understand the principle, the server side should send the data like this:

String message = "jsonp1294734708682 ({\" userid\ ": 0,\" username\ ": \" Null\ "})";

In this way, the JSON data {\ "userid\": 0,\ "username\": \ "Null\"} as a parameter to the jsonp1294734708682 callback function

Solution to invalid label when you get json across domains:

When the server side outputs Json data, precede the value of the callback parameter, such as: jsonp1294734708682 ({\ "userid\": 0,\ "username\": \ "Null\"})

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.