当前位置:网站首页>Cross domain data request using jsonp
Cross domain data request using jsonp
2022-06-29 07:29:00 【Yuan_ mingyu】
Preface : This example is deployed in XAMPP Site integration software package , stay localhost Test in the environment
1、 What is cross-domain
Because of the browser homology strategy , All requests sent url The agreement 、 domain name 、 Any one of the three ports is different from the current page address, which is cross domain . There are cross domain situations :
(1) Different network protocols , Such as http Agreement to access https agreement .
(2) Different ports , Such as 8080 Port access 3000 port .
(3) Domain name is different , Such as aaaa.com visit bbbb.com.
(4) Different subdomains , Such as java.ddd.com visit qianduan.ddd.com.
(5) Domain names correspond to domain names ip, Such as www.a.com visit 20.205.28.90.
2、Jsonp Method
because srript There is no cross domain label , So the use of script label , load src route , Implement cross domain loading js file . The js A method needs to be defined in the file , Pass the method name to the server , The server uses the method name , Assemble a method call statement , Respond to the browser . The browser executes the statement to call the method . Through parameters , Take out the response data .
(1) Native JS Request mode
The front-end code :
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = './js/text.js';
document.head.appendChild(script);
// Callback execution function
function onBack(res) {
console.log(res) //{name:" Xiao Ming ",age:24}
}
Back end code :
onBack({
name:" Xiao Ming ",
age:24
})
What needs to be noted here is , The data form of the back end must be used as the parameter of the function , And the function name should be consistent from front to back .
(2)Jquery Medium ajax Request mode
The front-end code :
$.ajax({
url:"./js/text.js",
type:"GET",
dataType:"jsonp", // Define the return method as jsonp
jsonpCallback:"onBack", // Define the callback function name , Keep the front and rear ends consistent
success:function(data){
console.log(data) //{name:" Xiao Ming ",age:24}
},
error:function(res){
console.log(" request was aborted !")
}
})
Back end code :
onBack({
name:" Xiao Ming ",
age:24
})
- shortcoming 1: Only use get request .
- shortcoming 2: Because it uses script label , There are safety risks .
- advantage : Support for older browsers , And can not support to CORS The website requests data .
original text :https://blog.csdn.net/weixin_43948229/article/details/86528111
边栏推荐
- Two ways to write throttling - recently seen
- 【科普资料】从科学精神到科学知识的材料
- How to view software testing training? Do you need training?
- Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
- Markdown 技能树(2):段落及强调
- SYSTEMd management node exporter
- 等保备案主体是谁?在当地网安进行备案是吗?
- Deploy Prometheus server service system management
- Spark RDD case: Statistics of daily new users
- cv2.cvtColor
猜你喜欢

Do you really understand "binder copy once"?
![[translation] E-Cloud. Large scale CDN using kubeedge](/img/ac/178c078589bb5bc16dbdc8f4ae9525.png)
[translation] E-Cloud. Large scale CDN using kubeedge

并发幂等性防抖
如何看待软件测试培训?你需要培训吗?

施努卡:3d机器视觉检测系统 3d视觉检测应用行业

Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2

利用IPv6实现公网访问远程桌面

微信小程序学习笔记(暑假)

How to talk about salary correctly in software test interview?

Autosar SWC在Simulink中Parameter的使用
随机推荐
Beanpostprocessor and beanfactorypostprocessor
systemd 管理node-exporter
机器学习笔记 - 时间序列使用机器学习进行预测
SYSTEMd management node exporter
Machine learning notes - time series prediction using machine learning
2022.6.27-----leetcode.522
WebRTC系列-网络传输之8-连通性检测
JVM系列之对象深度探秘
The realization of changing pop-up background at any time
Markdown 技能树(4):链接
LeetCode_ Dynamic programming_ Medium_ 91. decoding method
matlab 多普勒效应产生振动信号和处理
VPS是干嘛用的?有哪些知名牌子?与云服务器有什么区别?
Redis of NoSQL database (II): introduction to redis configuration file
KingbaseES应对表年龄增长过快导致事务回卷
jmeter 用beanshell导入自己jar包老是查找不到
uva10859
[QNX Hypervisor 2.2用户手册]6.2.1 Guest之间通信
项目中 if else 的代替写法
Is virtual DOM really the fastest?