在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 如何理解jQuery的ajax方法的參數(shù):jsonp和jsonpCallback

如何理解jQuery的ajax方法的參數(shù):jsonp和jsonpCallback(我并沒有問什么是jsonp)?

我并不是想問什么是jsonp,我想知道的僅僅是jQuery提供的ajax方法中,jsonp這個(gè)參數(shù)的意義~

回答
編輯回答
維她命

emmmmm……感覺問的有點(diǎn)廣——建議配合百度食用
簡單來說的話、jsonp是屬于一種跨域的數(shù)據(jù)獲取方式。
原理本質(zhì)上利用了<script>引用可跨域的形式。把連接當(dāng)成一個(gè)js或者css之類的引用,丟進(jìn)script再通過返回值來讀取。
而jsonpCallback是指你自己回調(diào)方法的方法名。因?yàn)閖sonp需要服務(wù)器端做配合。比如你訪問了一個(gè)接口。他返回的內(nèi)容實(shí)際是一串js代碼比如:

do("xxxxxxxx")

而這個(gè)do就是你的jsonpCallback。你需要自己另外再寫一個(gè)function do($str);在里面對(duì)數(shù)據(jù)進(jìn)行解析。

其實(shí)后來其實(shí)大部分都由服務(wù)器直接可以解決跨域問題了所以感覺jsonp就很少見了……

2017年1月2日 12:23
編輯回答
尋仙

jsonp是dataType的一個(gè)類型JSONP 格式
早期因WEB安全原因,Ajax默認(rèn)情況下是不能進(jìn)行跨域請(qǐng)求的 所以jsonp的意義就是簡潔說就是改變json數(shù)據(jù)的格式可以跨域 原理 可以具體自己去看

2017年9月2日 01:09
編輯回答
放開她

摘自 jQuery.ajax 文檔

jsonp
Type: String or Boolean
Override the callback function name in a JSONP request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {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" }. If you don't trust the target of your Ajax requests, consider setting the jsonp property to false for security reasons.

jsonpCallback
Type: String or Function()
Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.
2018年1月10日 14:16