Jquery Ajax cross-origin jsonp

Source: Internet
Author: User

The $. getjson method of jquery supports cross-origin.

Page request:

$. Getjson (URL + '? Callback =? ', {Operate: 'add'}, function (RES ){}});

It is actually another encapsulation of $. Ajax.

$. Ajax ({type: 'get', URL: URL, data :{}, datatype: 'jsonp', callback: 'json _ callback', success: function (RES) {}})

Server PHP:

$ Callback = $ _ Get ['callback']; print_r ("$ callback ({key: \" value \"});");

Principle:

You need to upload a callback function name to the server. The server obtains the callback function name and returns the returned data to the client in the form of parameters, so that the client can be tuned.

Callback =? That is, if the callback function name is not specified, jquery will automatically generate one, for example, jsonp12148037045654, which can be viewed using firebug.

When the server receives this callback, the return format is jsonp12148037045654 ({operate: 'add'}). It is not like the definition of a function.

Using $. Ajax to specify the callback parameter is equivalent to customizing this callback as "json_callback", which does not need to be automatically generated by jquery.

Ajax cross-origin requests do not support post. Remember.

Official jquery Website:

Http://api.jquery.com/jQuery.getJSON/#urldatacallback

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.