当前位置:网站首页>Cross domain and jsonp details
Cross domain and jsonp details
2022-07-06 04:07:00 【Concision.】
Cross domain and JSONP Detailed explanation
1. Homology
- If two pages agreement , Domain name and port All the same , The two pages have the same source .

for example , The following table shows the relativehttp://www.test.com/index.htmlPage homology detection :
2. The same-origin policy
The same-origin policy ( English full name Same origin policy) It's a browser Safety features . The same-origin policy Limit from The same source How the loaded document or script relates to the one from Another source Interact with other resources . This is an important security mechanism for isolating potentially malicious files .
3. Cross domain
- Homology refers to two URL The agreement 、 domain name 、 Port consistency , conversely , It's cross domain .
The root cause of cross domain: The same source strategy for browsers Don't allow Not homologous URL Interact with resources .
Browser interception of cross domain requests

How to implement cross domain data requests
- Implement cross domain data requests , The two main solutions :
JSONPandCORS.
JSONP: It's early , Compatibility is good. ( Compatible with lower versions IE). It's the front-end programmer to solve cross domain problems , Forced to come up with a temporary solution . The disadvantage is that it only supports GET request , I won't support it POST request .CORS: Late appearance , It is W3C standard , Cross domain Ajax The underlying solution to the request . Support GET and POST request . The disadvantage is that it is not compatible with some lower versions of browsers .
JSONP
JSONP(JSON with Padding) yes JSON A kind of “ Usage mode ”, can Used to solve the problem of cross domain data access of mainstream browsers .- Due to the restriction of browser homology strategy , Can't pass... In the web page Ajax Request non homologous interface data . however
<script>label Not affected by browser homology policy , Can passsrcattribute , Request non homologous js Script . Realization principle: It's through<script>Labeledsrcattribute , Request a cross domain data interface , And in the form of function calls , Receive the data returned from the cross domain interface response . For detailed use, please refer to The blog
<body>
<script>
function abc(data) {
console.log('JSONP The data returned in response is :')
console.log(data)
}
</script>
<script src="http://ajax.frontend.itheima.net:3006/api/jsonp?callback=abc&name=ls&age=30"></script>
</body>
</html>
- JSONP The shortcomings of
because JSONP It's through
<script>Labeled src attribute , To achieve cross domain data acquisition , therefore JSONP Only support GET Data request , I won't support it POST request .JSONP and Ajax There is no relationship between , Can't take JSONP The way to request data is called Ajax, because JSONP Not used XMLHttpRequest This object .
jQuery Medium JSONP
jQuery Provided $.ajax() function , In addition to initiating real Ajax In addition to data requests , Can also initiate JSONP Data request , for example :
$.ajax({
url: 'http://ajax.frontend.itheima.net:3006/api/jsonp?name=zs&age=20',
// If you want to use $.ajax() launch JSONP request , Must specify datatype by jsonp
dataType: 'jsonp',
success: function(res) {
console.log(res)
}
})
Be careful : By default , Use jQuery launch JSONP request , Will automatically carry a callback=jQueryxxx Parameters of ,jQueryxxx Is the name of a randomly generated callback function . If you want to customize JSONP The parameters of the callback function and the name of the callback function , You can specify... With the following two parameters :
$.ajax({
url: 'http://ajax.frontend.itheima.net:3006/api/jsonp?name=zs&age=20',
dataType: 'jsonp',
// Parameter name sent to the server , The default value is callback
jsonp: 'callback',
// Custom callback function name , The default value is jQueryxxx Format
jsonpCallback: 'abc',
success: function(res) {
console.log(res)
}
})
Implementation process 
边栏推荐
- 有条件地 [JsonIgnore]
- [FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
- How to execute an SQL statement in MySQL
- 20、 EEPROM memory (AT24C02) (similar to AD)
- Interface idempotency
- DM8 backup set deletion
- 10個 Istio 流量管理 最常用的例子,你知道幾個?
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
- 【按鍵消抖】基於FPGA的按鍵消抖模塊開發
- The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
猜你喜欢

Simple blog system

WPF effect Article 191 box selection listbox

Serial port-rs232-rs485-ttl

math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)

STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储

Web components series (VII) -- life cycle of custom components

在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒

Alibaba testers use UI automated testing to achieve element positioning

Custom event of C (31)

Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
随机推荐
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
颠覆你的认知?get和post请求的本质
C (XXIX) C listbox CheckedListBox Imagelist
Serial port-rs232-rs485-ttl
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
Ybtoj coloring plan [tree chain dissection, segment tree, tarjan]
记一次excel XXE漏洞
Proof of Stirling formula
Record the pit of NETCORE's memory surge
MySQL master-slave replication
Prime Protocol宣布在Moonbeam上的跨链互连应用程序
Basic knowledge of binary tree, BFC, DFS
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
asp. Core is compatible with both JWT authentication and cookies authentication
Detailed explanation of serialization and deserialization
Tips for using dm8huge table
About some basic DP -- those things about coins (the basic introduction of DP)
Introduction to data types in MySQL
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did