銆愯浆銆戝悓婧愮瓥鐣ュ拰璺ㄥ煙璇锋眰瑙e喅鏂规
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愯浆銆戝悓婧愮瓥鐣ュ拰璺ㄥ煙璇锋眰瑙e喅鏂规相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%a7%bb%e9%99%a4' title='绉婚櫎'>绉婚櫎
鍒锋柊 cdn cep 鍒╃敤 pdo OLE tar 瀹氬悜涓€銆佷竴涓簮鐨勫畾涔?/h1>
濡傛灉涓や釜椤甸潰鐨勫崗璁紝绔彛锛堝鏋滄湁鎸囧畾锛夊拰鍩熷悕閮界浉鍚岋紝鍒欎袱涓〉闈㈠叿鏈夌浉鍚岀殑婧愩€?br />涓句釜渚嬪瓙锛?/p>
涓嬭〃缁欏嚭浜嗙浉瀵筯ttp://a.xyz.com/dir/page.html鍚屾簮妫€娴嬬殑绀轰緥: URL 缁撴灉 鍘熷洜 http://a.xyz.com/dir2/other.html 鎴愬姛 鍗忚锛岀鍙o紙濡傛灉鏈夋寚瀹氾級鍜屽煙鍚嶉兘鐩稿悓 http://a.xyz.com/dir/inner/another.html 鎴愬姛 鍗忚锛岀鍙o紙濡傛灉鏈夋寚瀹氾級鍜屽煙鍚嶉兘鐩稿悓 https://a.xyz.com/secure.html 澶辫触 涓嶅悓鍗忚 ( https鍜宧ttp ) http://a.xyz.com:81/dir/etc.html 澶辫触 涓嶅悓绔彛 ( 81鍜?0) http://a.opq.com/dir/other.html 澶辫触 涓嶅悓鍩熷悕 ( xyz鍜宱pq)
浜屻€佸悓婧愮瓥鐣ユ槸浠€涔堬紵
鍚屾簮绛栫暐鏄祻瑙堝櫒鐨勪竴涓畨鍏ㄥ姛鑳斤紝涓嶅悓婧愮殑瀹㈡埛绔剼鏈湪娌℃湁鏄庣‘鎺堟潈鐨勬儏鍐典笅锛屼笉鑳借鍐欏鏂硅祫婧愩€傛墍浠yz.com涓嬬殑js鑴氭湰閲囩敤ajax璇诲彇abc.com閲岄潰鐨勬枃浠舵暟鎹槸浼氳鎷掔粷鐨勩€?/p>
鍚屾簮绛栫暐闄愬埗浜嗕粠鍚屼竴涓簮鍔犺浇鐨勬枃妗f垨鑴氭湰濡備綍涓庢潵鑷彟涓€涓簮鐨勮祫婧愯繘琛屼氦浜掋€傝繖鏄竴涓敤浜庨殧绂绘綔鍦ㄦ伓鎰忔枃浠剁殑閲嶈瀹夊叏鏈哄埗銆?/p>
涓夈€佸熀浜巎sonp瀹炵幇鐨勮法鍩熻姹?/h1>
-
椤甸潰涓殑閾炬帴锛岄噸瀹氬悜浠ュ強琛ㄥ崟鎻愪氦鏄笉浼氬彈鍒板悓婧愮瓥鐣ラ檺鍒剁殑銆?/p>
-
璺ㄥ煙璧勬簮鐨勫紩鍏ユ槸鍙互鐨勩€備絾鏄痡s涓嶈兘璇诲啓鍔犺浇鐨勫唴瀹广€傚宓屽叆鍒伴〉闈腑鐨?lt;script src="..."></script>锛?lt;img>锛?lt;link>锛?lt;iframe>绛夈€?/p>
椤甸潰涓殑閾炬帴锛岄噸瀹氬悜浠ュ強琛ㄥ崟鎻愪氦鏄笉浼氬彈鍒板悓婧愮瓥鐣ラ檺鍒剁殑銆?/p>
璺ㄥ煙璧勬簮鐨勫紩鍏ユ槸鍙互鐨勩€備絾鏄痡s涓嶈兘璇诲啓鍔犺浇鐨勫唴瀹广€傚宓屽叆鍒伴〉闈腑鐨?lt;script src="..."></script>锛?lt;img>锛?lt;link>锛?lt;iframe>绛夈€?/p>
涓嬮潰鏉ュ垎姝ヤ妇渚嬭缁嗛槓杩板叾涓殑濂ュ锛?/p>
1銆佸厛寮€涓や釜椤圭洰锛?/h3>
椤圭洰1锛坔ttp://127.0.0.1:8000/锛?br />椤圭洰2锛坔ttp://127.0.0.1:8100/锛?/p>
椤圭洰1
url锛? url(r鈥榠ndex1/$鈥?span style="color: #000000;">,views.index1) views锛? def index1(request): return HttpResponse(鈥榳angjifei鈥?
椤圭洰2
url锛? url(r鈥榠ndex2/$鈥?views.index2) views 锛? def index2(request): return render(request,鈥榠ndex2.html鈥? index2.html锛? <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> $(鈥?/span>#btn鈥?/span>).click(function () { $.ajax({ url:"http://127.0.0.1:8000/index1/", type:鈥?/span>get鈥?/span>, success:function (res) { console.log(res) } }) }) </script> </body> </html>
鐜板湪锛屾墦寮€浣跨敤娴忚鍣ㄦ墦寮€ http://127.0.0.1:8100/index2/锛岀偣鍑婚〉闈笂鐨?鈥樻彁浜も€?鎸夐挳锛屼細鍦╟onsole椤甸潰鍙戠幇閿欒淇℃伅濡備笅锛?/p>
涓轰粈涔堟姤閿欏憿锛熷洜涓哄悓婧愮瓥鐣ラ檺鍒惰法鍩熷彂閫乤jax璇锋眰銆?br />缁嗗績鐐圭殑鍚屽搴旇浼氬彂鐜版垜浠殑demo1椤圭洰鍏跺疄宸茬粡鎺ユ敹鍒颁簡璇锋眰骞惰繑鍥炰簡鍝嶅簲锛屾槸娴忚鍣ㄥ闈炲悓婧愯姹傝繑鍥炵殑缁撴灉鍋氫簡鎷︽埅銆?br />鍐嶇粏蹇冪偣鐨勫悓瀛︿細鍙戠幇锛屾垜浠娇鐢╟dn鏂瑰紡寮曠敤鐨刯Query鏂囦欢涔熸槸璺ㄥ煙鐨勶紝瀹冨氨鍙互浣跨敤銆?br />鍚屾牱鏄粠鍏朵粬鐨勭珯鐐规嬁涓滆タ锛宻cript鏍囩灏卞彲浠ャ€傞偅鎴戜滑鑳戒笉鑳藉埄鐢ㄨ繖涓€鐐规悶鐐逛簨鎯呭憿锛?/p>
2銆佹妸index2.html涓殑浠g爜鏀逛竴涓嬶細
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script src="http://127.0.0.1:8000/index1/"></script> </body> </html>
鐜板湪鍒锋柊涓€涓嬩細鍑虹幇濡備笅閿欒锛?/p>
鐪嬫潵鍚庣杩斿洖鐨勫搷搴斿凡缁忚鎷垮埌浜嗭紝鍙笉杩囨妸wangjifei褰撴垚浜嗕竴涓彉閲忔潵浣跨敤锛屼絾鏄椤甸潰涓婂嵈娌℃湁瀹氫箟涓€涓悕涓簑angjifei鐨勫彉閲忋€傛墍浠ュ嚭閿欎簡銆?/p>
3銆侀偅鎴戜滑灏卞湪index2.html涓畾涔変竴涓獁angjifei鍙橀噺鐪嬬湅锛?/h3>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鍚屾簮绛栫暐</title>
</head>
<body>
<button id="btn">鎻愪氦</button>
<script>
var wangjifei = 123
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script>
<script src="http://127.0.0.1:8000/index1/"></script>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script> var wangjifei = 123 </script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script src="http://127.0.0.1:8000/index1/"></script> </body> </html>
鍒锋柊鍙戠幇涓嶆姤閿欎簡锛?/p>
4銆佹垜瀹氫箟涓€涓彉閲忓彲浠ワ紝閭e彲涓嶅彲浠ュ畾涔変竴涓嚱鏁板憿锛?/h3>
index2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script> function wangjifei() { console.log(鈥?/span>鍑烘墜灏辫涓撲笟鈥?/span>) } </script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script src="http://127.0.0.1:8000/index1/"></script> </body> </html>
椤圭洰1涓殑views锛氫篃淇敼涓€涓?/p>
def index1(request): return HttpResponse(鈥榳angjifei()鈥?
鍒锋柊涓€涓嬮〉闈㈡樉绀虹粨鏋滐細
缁撴灉鍒嗘瀽锛氳繑鍥炵殑 wangjifei()锛岄〉闈笂鎷垮埌杩欎釜鍝嶅簲涔嬪悗鐩存帴鎵ц浜唚angjifei鍑芥暟锛?/p>
5銆侀偅鍑芥暟涓彲涓嶅彲浠ヤ紶閫掑弬鏁板憿锛熸垜浠瘯涓€涓嬶紒
index2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script> function wangjifei(res) { console.log(res) } </script> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script src="http://127.0.0.1:8000/index1/"></script> </body> </html>
椤圭洰1涓殑 views
from django.http import HttpResponse import json def index1(request): ret={鈥榗ode鈥?1,鈥榤sg鈥?[110,119,120,12306]} res = json.dumps(ret) return HttpResponse(f鈥榳angjifei({res})鈥?
鍒锋柊涔嬪悗鏄剧ず缁撴灉锛?/p>
鏋滅劧浼犻€掑弬鏁颁篃鏄彲浠ョ殑锛佹垜浠€氳繃script鏍囩鐨勮法鍩熺壒鎬ф潵缁曡繃鍚屾簮绛栫暐鎷垮埌鎯宠鐨勬暟鎹簡锛侊紒锛?/p>
杩欏叾瀹炲氨鏄疛SONP鐨勭畝鍗曞疄鐜版ā寮忥紝鎴栬€呰鏄疛SONP鐨勫師鍨嬶細鍒涘缓涓€涓洖璋冨嚱鏁帮紝鐒跺悗鍦ㄨ繙绋嬫湇鍔′笂璋冪敤杩欎釜鍑芥暟骞朵笖灏咼SON 鏁版嵁褰㈠紡浣滀负鍙傛暟浼犻€掞紝瀹屾垚鍥炶皟銆?br />灏咼SON鏁版嵁濉厖杩涘洖璋冨嚱鏁帮紝杩欏氨鏄疛SONP鐨凧SON+Padding鐨勫惈涔夈€?br />浣嗘槸鎴戜滑鏇村鏃跺€欐槸甯屾湜閫氳繃浜嬩欢瑙﹀彂鏁版嵁鐨勮幏鍙栵紝鑰屼笉鏄儚涓婇潰涓€鏍烽〉闈竴鍒锋柊灏辨墽琛屼簡锛岃繖鏍峰緢涓嶇伒娲汇€?/p>
6銆佹垜浠彲浠ラ€氳繃javascript鍔ㄦ€佺殑鍒涘缓script鏍囩鏉ュ疄鐜般€?/h3>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鍚屾簮绛栫暐</title>
</head>
<body>
<button id="btn">鎻愪氦</button>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script>
<script>
//鑷畾涔夌殑鍑芥暟
function wangjifei(res) {
console.log(res)
}
//jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢
$(鈥?btn鈥?.click(function () {
//鍒涘缓涓€涓猻cript鏍囩
var scriptEle = document.createElement(鈥榮cript鈥?span style="color: #000000;">);
//缁欐爣绛炬坊鍔爏rc灞炴€э紝骞舵坊鍔犲搴旂殑灞炴€у€? http://127.0.0.1:8000/index1
$(scriptEle).attr(鈥榮rc鈥?鈥榟ttp://127.0.0.1:8000/index1鈥?span style="color: #000000;">);
//灏嗗垱寤哄ソ鐨勬爣绛炬坊鍔犲埌椤甸潰涓紝鏍囩娣诲姞鍚庡氨浼氳嚜鍔ㄨЕ鍙慻et璇锋眰
$(鈥榖ody鈥?span style="color: #000000;">).append(scriptEle);
//灏嗘爣绛剧Щ闄?/span>
$(scriptEle).remove()
})
</script>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> //鑷畾涔夌殑鍑芥暟 function wangjifei(res) { console.log(res) } //jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢 $(鈥?btn鈥?.click(function () { //鍒涘缓涓€涓猻cript鏍囩 var scriptEle = document.createElement(鈥榮cript鈥?span style="color: #000000;">); //缁欐爣绛炬坊鍔爏rc灞炴€э紝骞舵坊鍔犲搴旂殑灞炴€у€? http://127.0.0.1:8000/index1 $(scriptEle).attr(鈥榮rc鈥?鈥榟ttp://127.0.0.1:8000/index1鈥?span style="color: #000000;">); //灏嗗垱寤哄ソ鐨勬爣绛炬坊鍔犲埌椤甸潰涓紝鏍囩娣诲姞鍚庡氨浼氳嚜鍔ㄨЕ鍙慻et璇锋眰 $(鈥榖ody鈥?span style="color: #000000;">).append(scriptEle); //灏嗘爣绛剧Щ闄?/span> $(scriptEle).remove() }) </script> </body> </html>
杩欐牱褰撴垜浠偣鍑籦utton鎸夐挳鐨勬椂鍊欙紝浼氬湪椤甸潰涓婃彃鍏ヤ竴涓猻cript鏍囩锛岀劧鍚庝粠鍚庣鑾峰彇鏁版嵁鍚庡啀鍒犻櫎鎺夈€?/p>
7銆佷负浜嗗疄鐜版洿鍔犵伒娲荤殑璋冪敤锛屾垜浠彲浠ユ妸瀹㈡埛绔畾涔夌殑鍥炶皟鍑芥暟鐨勫嚱鏁板悕浼犵粰鏈嶅姟绔紝鏈嶅姟绔垯浼氳繑鍥炰互璇ュ洖璋冨嚱鏁板悕锛屽皢鑾峰彇鐨刯son鏁版嵁浼犲叆杩欎釜鍑芥暟瀹屾垚鍥炶皟銆傝繖鏍峰氨鑳藉疄鐜板姩鎬佺殑璋冪敤浜嗐€備慨鏀逛唬鐮佸涓?
index2.html浠g爜锛?/p>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> //鑷畾涔夌殑鍑芥暟 function xxx(res) { console.log(res) } //jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢 $(鈥?/span>#btn鈥?/span>).click(function () { //鍒涘缓涓€涓猻cript鏍囩 var scriptEle = document.createElement(鈥?/span>script鈥?/span>); //缁欐爣绛炬坊鍔爏rc灞炴€э紝骞舵坊鍔犲搴旂殑灞炴€у€? http://127.0.0.1:8000/index1锛焎allback=xxx $(scriptEle).attr(鈥?/span>src鈥?/span>,鈥?/span>http://127.0.0.1:8000/index1?callback=xxx鈥?/span>); //灏嗗垱寤哄ソ鐨勬爣绛炬坊鍔犲埌椤甸潰涓紝鏍囩娣诲姞鍚庡氨浼氳嚜鍔ㄨЕ鍙慻et璇锋眰 $(鈥?/span>body鈥?/span>).append(scriptEle); //灏嗘爣绛剧Щ闄?/span> $(scriptEle).remove() }) </script> </body> </html>
椤圭洰1涓璿iews锛?/p>
from django.http import HttpResponse import json def index1(request): ret={鈥榗ode鈥?1,鈥榤sg鈥?[110,119,120,12306]} res = json.dumps(ret) callback = request.GET.get(鈥榗allback鈥?span style="color: #000000;">) return HttpResponse(f鈥榹callback}({res})鈥?
鍥涖€乯Query涓璯etJSON鏂规硶浠嬬粛锛?/h1>
1銆乯Query涓湁涓撻棬鐨勬柟娉曞疄鐜癹sonp銆?/h3>
index2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> //jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢 $(鈥?/span>#btn鈥?/span>).click(function () { $.getJSON("http://127.0.0.1:8000/index1?callback=?",function (res) { console.log(res) }) }) </script> </body> </html>
瑕佹敞鎰忕殑鏄湪url鐨勫悗闈㈠繀椤昏鏈変竴涓猚allback鍙傛暟锛岃繖鏍穏etJSON鏂规硶鎵嶄細鐭ラ亾鏄敤JSONP鏂瑰紡鍘昏闂湇鍔★紝callback鍚庨潰鐨勯偅涓紵鏄痡Query鍐呴儴鑷姩鐢熸垚鐨勪竴涓洖璋冨嚱鏁板悕銆?/p>
2銆佷絾鏄鏋滄垜浠兂鑷繁鎸囧畾鍥炶皟鍑芥暟鍚嶏紝鎴栬€呰鏈嶅姟涓婅瀹氫簡鍥炶皟鍑芥暟鍚嶈鎬庝箞鍔炲憿锛熸垜浠彲浠ヤ娇鐢?.ajax鏂规硶鏉ュ疄鐜帮細
index2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> //jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢 $(鈥?/span>#btn鈥?/span>).click(function () { $.ajax({ //瑕佽闂殑url url:"http://127.0.0.1:8000/index1/", //瑕佸鐞嗙殑鏁版嵁绫诲瀷jsonp dataType:鈥?/span>jsonp鈥?/span>, //鑷畾涔夊洖璋冨嚱鏁板悕蹇呰鍙傛暟 jsonp:鈥?/span>callback鈥?/span>, //鑷畾涔夊洖璋冨嚱鏁板悕锛寀rl涓璫allback=鍚庨潰鐨勫嚱鏁板悕 jsonpcallback:鈥?/span>wangjifei鈥?/span> }) }); //鍥炶皟鍑芥暟 function wangjifei(res) { console.log(res) } </script> </body> </html>
views:
from django.http import HttpResponse import json def index1(request): ret={鈥榗ode鈥?1,鈥榤sg鈥?[110,119,120,12306]} res = json.dumps(ret) callback = request.GET.get(鈥榗allback鈥?span style="color: #000000;">) return HttpResponse(f鈥榳angjifei({res})鈥?
3銆佺敤ajax鎶€鏈€氬父灏嗗洖璋冨嚱鏁板啓鍦ㄦ垚鍔熷洖璋冨嚱鏁扮殑浣嶇疆锛?/h3>
index2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="btn">鎻愪氦</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"> </script> <script> //jquery缁檅utton缁戝畾鐐瑰嚮浜嬩欢 $(鈥?/span>#btn鈥?/span>).click(function () { $.ajax({ //瑕佽闂殑url url:"http://127.0.0.1:8000/index1/", //瑕佸鐞嗙殑鏁版嵁绫诲瀷jsonp dataType:鈥?/span>jsonp鈥?/span>, //success鍥炶皟 success:function (res) { console.log(res) } }) }); //鍥炶皟鍑芥暟 function wangjifei(res) { console.log(res) } </script> </body> </html>
鏈€鍚庢潵涓€涓猨sonp鐨勫疄闄呭簲鐢細
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鍚屾簮绛栫暐</title> </head> <body> <button id="show-tv">鎻愪氦</button> <div class="tv-list"></div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> $("#show-tv").click(function () { $.ajax({ url: "http://www.jxntv.cn/data/jmd-jxtv2.html? callback=list&_=1454376870403", dataType: 鈥?/span>jsonp鈥?/span>, jsonp: 鈥?/span>callback鈥?/span>, jsonpCallback: 鈥?/span>list鈥?/span>, success: function (data) { var weekList = data.data; console.log(weekList); var $tvListEle = $(".tv-list"); $.each(weekList, function (k, v) { var s1 = "<p>" + v.week + "鍒楄〃</p>"; $tvListEle.append(s1); $.each(v.list, function (k2, v2) { var s2 = "<p><a href=鈥?/span>" + v2.link + "鈥?gt;" + v2.name + "</a></p>"; $tvListEle.append(s2) }); $tvListEle.append("<hr>"); }) } }) }); </script> </body> </html>
浜斻€佸熀浜嶤ore鏂规硶瑙e喅璺ㄥ煙璇锋眰
-
鎴戜滑浠嬬粛浜唈sonp瑙e喅璺ㄥ煙璇锋眰闂锛岃繖绉嶈В鍐虫柟寮忓緢濂界殑璇犻噴浜嗚法鍩熻姹傜殑鏈川锛屼絾鏄暐鏄鹃夯鐑︼紝鏄惁杩樿寰楀湪鎴戜滑涓嶅仛浠讳綍澶勭悊鐨勬椂鍊欙紝璺ㄥ煙璇锋眰鏃跺€欐祻瑙堝櫒缁欐垜浠姤鐨勯敊璇笉锛熺炕璇戣繃鏉ュ氨鏄洜涓哄搷搴斿ご娌℃湁鎸囧畾Access-Control-Allow-Origin鎵€鍏佽鍘熷鐨勮姹傝矾寰勶紝鍥犳鍘熷璇锋眰璺緞http://127.0.0.1:8001涓嶈鍏佽璁块棶銆?鍩轰簬涓婅堪鐨勫師鍥犺В閲婏紝鎴戜滑鍙渶瑕佸湪鍝嶅簲鐨勫唴瀹瑰姞鍏ヤ笂杩拌繖鏍风殑鎺堟潈瀛楁锛屼究鍙В鍐炽€?/p>
-
绠€鍗曡姹傜殑瀹氫箟锛?br />鍙鍚屾椂婊¤冻浠ヤ笅涓ゅぇ鏉′欢锛屽氨灞炰簬绠€鍗曡姹?涓嶆弧瓒冲氨鏄鏉傝姹傦紒锛侊紒
1.锛?锛?璇锋眰鏂规硶鏄互涓嬩笁绉嶆柟娉曚箣涓€锛?br />-- HEAD锛孏ET锛孭OST
2.锛?锛塇TTP鐨勫ご淇℃伅涓嶈秴鍑轰互涓嬪嚑绉嶅瓧娈碉細-- Accept
-- Accept-Language
-- Content-Language
-- Last-Event-ID
-- Content-Type锛氬彧闄愪簬涓変釜鍊糰pplication/x-www-form-urlencoded銆乵ultipart/form-data銆乼ext/plain
鐢变簬django鐨勬墍鏈夎姹傚搷搴旈兘瑕佽蛋涓棿浠讹紝鎵€浠ュ彲浠ュ啓涓€涓法鍩熺殑涓棿浠舵潵瑙e喅璺ㄥ煙闂
from django.utils.deprecation import MiddlewareMixin class MyCore(MiddlewareMixin): def process_response(self, request, response): response[鈥楢ccess-Control-Allow-Origin鈥榏 = "*" //绠€鍗曡姹?/span> if request.method == "OPTIONS": # 澶嶆潅璇锋眰 棰勬 response[鈥楢ccess-Control-Allow-Headers鈥榏 = "Content-Type" response[鈥楢ccess-Control-Allow-Methods鈥榏 = "POST, DELETE, PUT" return response