当前位置:网站首页>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
边栏推荐
猜你喜欢

Redis (4) of NoSQL database: redis new data type

Appium自动化测试基础 — ADB常用命令(二)

cv2.cvtColor
What tools do testers need to know

Deploy Prometheus server service system management

The realization of changing pop-up background at any time

matlab simulink 电网扫频仿真和分析

Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.

部署Prometheus-server服务 system管理
如何看待软件测试培训?你需要培训吗?
随机推荐
Use of parameter in Simulink for AUTOSAR SWC
tf.to_int64
golang 修改 结构体切片的值
阿里云访问资源:NoSuchKey
Markdown 技能树(8):代码块
uva10859
National Security Agency and CISA kubernetes reinforcement guidelines - new content in version 1.1
利用IPv6實現公網訪問遠程桌面
查看tensorflow是否支持GPU,以及测试程序
Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
SAP UI5 初学 ( 一 )、简介
机器学习笔记 - 时间序列的混合模型
你真的懂 “Binder 一次拷贝吗“?
Webrtc series - 8-connectivity detection for network transmission
Golang modifying the value of a structure slice
Spark RDD案例:统计每日新增用户
Markdown 技能树(4):链接
关联性——相关性分析
Twitter launches the test of anti abuse tool "safe mode" and adds enabling prompt
excel高级绘图技巧100讲(六)-甘特图在项目进度上的实战应用案例