Home > Web Front-end > JS Tutorial > body text

Solution to invalid label error when Jquery obtains Json across domains_jquery

WBOY
Release: 2016-05-16 18:12:17
Original
750 people have browsed it

Finally, calm down carefully and read the json official document carefully and found this paragraph:

JSON data is a kind of structured data that can be easily parsed through JavaScript. If the obtained data file is stored on a remote server (with different domain names, that is, cross-domain data acquisition), you need to use the jsonp type. Using this type creates a query string parameter callback=? which is appended to the requested URL. The server should add the callback function name before the JSON data 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 $.ajax().

In fact, the principle of jquery cross-domain is realized through external links <script>, and then the callback function is added with the parameters of the callback function to achieve true cross-domain <br><br>Jquery is used every time When sending a request across domains, there will be a callback parameter. In fact, the value of this parameter is the name of the callback function. Therefore, when the server sends json data, it should put this parameter in front. The value of this parameter is often randomly generated, such as : jsonp1294734708682, and you can also set the name of the callback method through the $.ajax method. After understanding the principle, the server should send data like this: <br><br>string message = "jsonp1294734708682({"userid":0,"username":"null"})"; <br><br>This way, The json data {"userid":0,"username":"null"} is used as a parameter of the jsonp1294734708682 callback function <br><br>Solution to invalid label when obtaining Json across domains: <br><br>Server When the terminal outputs Json data, add the value of the callback parameter in front, such as: jsonp1294734708682({"userid":0,"username":"null"})</script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!