jquery ajax how to use cross-domain access jsonp

In the interface used in the project are more cross-domain access client, jquery jquery ajax can only use the jsonp method.

It is noteworthy that, jQuery.ajax () only supports cross-domain, post way to get approach is not supported.
<pre>
<Script of the type = "text / JavaScript">

// shorthand
$ .getJSON ( "http://www.shuchengxian.com/index.php?callback=?",
function (the Data) {
$ ( "# . Show ") text (Data)
});

complete form //
$ .ajax ({
the async: to false,
URL:, // cross-domain 'http://www.shuchengxian.com/index.php?callback=?' URL callback parameters required
type: 'the GET',
dataType: 'JSONP',
JSONP: 'jsoncallback', // default the callback
data: mydata, // request data
timeout: 5000,
beforeSend: function () {// this embodiment JSONP the method will not be triggered. the reason may be designated as if dataType jsonp, then it is not the event ajax
},
success: After function (json) {// the client predefined callback function ends jquery, json successfully acquired cross-domain data on the server, the callback function will be performed dynamically
IF (json.length = 0!) {
Alert (json. ActionErrors);
}
},
error: function (XHR) {
// JSONP of this method is not triggered
@ request error process
alert ( "request error (check network status correlation.)");
}
});
</ Script>
</ pre>
in this manner the embodiment actually $ .ajax ({..}) an advanced package.

In the callback parameter is obtained by the server (such as: jsonpcallback) obtained subsequent to the end jQuery callback,

Similar then return: "jsonpcallback (" + json array to be returned + ")";

jquery callback method will call this dynamic loading: jsonpcallback (json array);

If jQuery default, the data was randomly jsonp1356493334400 like. This will achieve the purpose of cross-domain data exchange.

JSONP is a script injection (Script Injection) behavior, there are some security risks.

Note: jquey does not support post way across domains.

It is generally not recommended jsonp but cross-domain access control domain names under PHP backend to be accessed can be added at no cross-domain
<pre>
// handle cross-domain
header ( "Access-Control-Allow -Origin: *"); / / * number indicates that all domain can access the
header ( "access-Control-Method, the Allow-: POST, GET");
</ pre>

Can get with PHP curl can also be cross-domain access

Guess you like

Origin www.cnblogs.com/newmiracle/p/11875975.html