当前位置:网站首页>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
边栏推荐
- 树形下拉选择框el-select结合el-tree效果demo(整理)
- What is a Test Architect
- Markdown skill tree (1): introduction to markdown
- 期末总结——Spark
- Machine learning notes - time series prediction using machine learning
- SAP UI5 初学 ( 一 )、简介
- YGG cooperated with Web3 platform leader to empower the creative community with Dao tools and resources
- Is virtual DOM really the fastest?
- 服裝行業的CRM品牌供應商如何選型?
- [translation] E-Cloud. Large scale CDN using kubeedge
猜你喜欢
随机推荐
Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
Explain canfd message and format in AUTOSAR arxml in detail
【翻译】e-Cloud。使用KubeEdge的大规模CDN
游标长时间open导致表无法vacuum问题
What tools do testers need to know
软件测试面试如何正确谈论薪资?
How to select CRM brand suppliers in garment industry?
利用IPv6实现公网访问远程桌面
[popular science materials] materials from scientific spirit to scientific knowledge
2022.6.27-----leetcode.522
Alternative writing of if else in a project
[translation] how Bink drives the digital loyalty transactions of some of the largest banks in the UK
Shift/space studio "Aurora" project: building a villa in the sandbox metauniverse
golang 修改 结构体切片的值
SAP UI5 初学 ( 一 )、简介
YGG pilipinas: typhoon Odette disaster relief work update
matlab simulink 电网扫频仿真和分析
uva10891
Machine learning notes - time series prediction using machine learning
Markdown skill tree (6): List






