How to solve the invalid label error when Jquery obtains Json cross-Origin

Source: Internet
Author: User

Finally, take a closer look and read the official json document to find the following paragraph:

JSON data is a structured data that can be easily parsed using JavaScript. If the obtained data file is stored on a remote server (the domain name is different, that is, cross-Origin data retrieval), The jsonp type is required. If this type is used, a query string parameter callback =? will be created? This parameter is added after the request URL. The server should add the callback function name before the JSON data to complete a valid JSONP request. If you want to specify the callback function parameter name to replace the default callback, you can set the jsonp parameter of $. ajax.

In fact, the cross-origin principle of jquery is implemented through the external chain <script>, and then the callback function parameters are added to realize the real cross-origin.

Jquery will have the callback parameter each time it sends a request across domains. In fact, the value of this parameter is the name of the callback function. Therefore, when sending json data, the server should put this parameter in front, the value of this parameter is often randomly generated, for example, jsonp129472138682. You can also use $. set the name of the callback method in ajax. After understanding the principles, the server should send data as follows:

String message = "Maid ({\" userid \ ": 0, \" username \ ": \" null \"})";

In this way, json data {\ "userid \": 0, \ "username \": \ "null \"} is used as a parameter of the jsonp129472138682 callback function.

Solution to invalid label when obtaining Json from cross-origin:

When the server outputs Json data, add the callback parameter value, for example, jsonp129472138682 ({\ "userid \": 0, \ "username \": \ "null \"})

Related Article

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.