jQuery 中的JSONPjquery
// Using YQL and JSONP $.ajax({ url: "http://query.yahooapis.com/v1/public/yql", // The name of the callback parameter, as specified by the YQL service jsonp: "callback", // Tell jQuery we're expecting JSONP dataType: "jsonp", // Tell YQL what we want and that we want JSON data: { q: "select title,abstract,url from search.news where query=\"cat\"", format: "json" }, // Work with the response success: function( response ) { console.log( response ); // server response } });
JQ這裏的額設置容易引發混淆 dataType是jsonpajax
例子來源 https://learn.jquery.com/ajax/working-with-jsonp/json
API http://api.jquery.com/jquery.ajax/api
若是是指定了回調函數名 app
dataType:"jsonp"
: less
"jsonp"
: Loads in a JSON block using JSONP. Adds an extra "?callback=?"
to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]"
, to the URL unless the cache
option is set to true
.
另外還有兩個參數能夠配置dom
{jsonp:'onJSONPLoad'}
would result in
'onJSONPLoad=?'
passed to the server.
As of jQuery 1.5, setting the
jsonp
option to
false
prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the
jsonpCallback
setting. For example,
{ jsonp: false, jsonpCallback: "callbackName" }
jsonpCallback
is set to the return value of that function.
第一段 若是你的url有指定callback的名字 這裏重寫了 注意! xxx=fun 變成了 valueYouSet=funide
第二段 指定回調函數函數