当前位置:网站首页>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.html
Page 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 :
JSONP
andCORS
.
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 passsrc
attribute , Request non homologous js Script . Realization principle
: It's through<script>
Labeledsrc
attribute , 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
边栏推荐
- 【按键消抖】基于FPGA的按键消抖模块开发
- Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
- Tips for using dm8huge table
- C form application of C (27)
- Introduction to data types in MySQL
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- Pandora IOT development board learning (HAL Library) - Experiment 9 PWM output experiment (learning notes)
- 判断当天是当月的第几周
- Detailed explanation of serialization and deserialization
- C#(三十一)之自定义事件
猜你喜欢
简易博客系统
KS008基于SSM的新闻发布系统
WPF效果第一百九十一篇之框选ListBox
[disassembly] a visual air fryer. By the way, analyze the internal circuit
C language -- structs, unions, enumerations, and custom types
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
Serial port-rs232-rs485-ttl
[001] [stm32] how to download STM32 original factory data
Record the pit of NETCORE's memory surge
KS003基于JSP和Servlet实现的商城系统
随机推荐
Determine which week of the month the day is
Maxay paper latex template description
Interface idempotency
MySql数据库root账户无法远程登陆解决办法
Redis (replicate dictionary server) cache
Cf464e the classic problem [shortest path, chairman tree]
MySQL transaction isolation level
C language -- structs, unions, enumerations, and custom types
KS003基于JSP和Servlet实现的商城系统
C form application of C (27)
记一次excel XXE漏洞
How does technology have the ability to solve problems perfectly
【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
About some basic DP -- those things about coins (the basic introduction of DP)
DM8 archive log file manual switching
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Oracle ORA error message
TCP/IP协议里面的网关地址和ip地址有什么区别?
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes