当前位置:网站首页>jsonp原理
jsonp原理
2022-07-16 07:52:00 【小阿狸啊】
利用 script 標簽沒有跨域限制的漏洞,網頁可以得到從其他來源動態產生的 JSON 數據。JSONP
請求一定需要對方的服務器做支持才可以。
- JSONP和AJAX對比
JSONP和AJAX相同,都是客戶端向服務器端發送請求,從服務器端獲取數據的方式。但AJAX屬於
同源策略,JSONP屬於非同源策略(跨域請求) - JSONP優缺點
JSONP優點是簡單兼容性好,可用於解决主流瀏覽器的跨域數據訪問的問題。缺點是僅支持get方
法具有局限性,不安全可能會遭受XSS攻擊。 - JSONP的實現流程
- 聲明一個回調函數,其函數名(如show)當做參數值,要傳遞給跨域請求數據的服務器,函數形參
為要獲取目標數據(服務器返回的data)。 - 創建一個``標簽,把那個跨域的API數據接口地址,賦值給script的src,還要在這個地址中向服務器
傳遞該函數名(可以通過問號傳參:?callback=show)。 - 服務器接收到請求後,需要進行特殊的處理:把傳遞進來的函數名和它需要給你的數據拼接成一個
字符串,例如:傳遞進去的函數名是show,它准備好的數據是 show(‘我不愛你’) 。 - 最後服務器把准備的數據通過HTTP協議返回給客戶端,客戶端再調用執行之前聲明的回調函數
(show),對返回的數據進行操作。
在開發中可能會遇到多個 JSONP 請求的回調函數名是相同的,這時候就需要自己封裝一個 JSONP
函數。
// index.html
function jsonp({
url, params, callback }) {
return new Promise((resolve, reject) => {
let script = document.createElement('script')
window[callback] = function(data) {
resolve(data)
document.body.removeChild(script)
}
params = {
...params, callback } // wd=b&callback=show
let arrs = []
for (let key in params) {
arrs.push(`${
key}=${
params[key]}`)
}
script.src = `${
url}?${
arrs.join('&')}`
document.body.appendChild(script)
})
}
jsonp({
url: 'http://localhost:3000/say',
params: {
wd: 'Iloveyou' },
callback: 'show'
}).then(data => {
console.log(data)
})
上面這段代碼相當於向 http://localhost:3000/say?wd=Iloveyou&callback=show 這個地址請求數
據,然後後臺返回 show(‘我不愛你’) ,最後會運行show()這個函數,打印出’我不愛你’
// server.js
let express = require('express')
let app = express()
app.get('/say', function(req, res) {
let {
wd, callback } = req.query
console.log(wd) // Iloveyou
console.log(callback) // show
res.end(`${
callback}('我不愛你')`)
})
app.listen(3000)
- jQuery的jsonp形式
JSONP都是GET和异步請求的,不存在其他的請求方式和同步請求,且jQuery默認就會給JSONP的請求清除緩存。
$.ajax({
url:"http://crossdomain.com/jsonServerResponse",
dataType:"jsonp",
type:"get",//可以省略
jsonpCallback:"show",//->自定義傳遞給服務器的函數名,而不是使用jQuery自動生成的,可省略
jsonp:"callback",//->把傳遞函數名的那個形參callback,可省略
success:function (data){
console.log(data);}
});
边栏推荐
- CentOS 7X 安装Mysql 数据库
- Pytest series-01-installation and introduction
- Data storage and disaster recovery (2nd Edition) editor in chief Lu Xianzhi Wu Chunling comprehensive training answer
- C#笔记-基础知识,问答,WPF
- 华为全连MGRE与星型拓扑MGRE(全网状与非全网状)
- MySQL learning records
- Detailed explanation of sliding window
- 递归实现指数型枚举
- Re 正则表达式
- 3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
猜你喜欢

2-3 tree B tree b+ tree

Setting the login interface in JMeter can only be called once

26 years old, has worked in automation for three years, and the monthly salary is only 12K. Can you change jobs and find a higher salary job?

Looking at "money" ~ the experience of a tester with two years' graduation and an annual salary of 30W

Day 6 of DL

利用 Redis 的 sorted set 做每周热评的功能

Day 5 of DL

重发布实验

druid数据库连接池监控页面

文件管理-阿里云OSS学习(一)
随机推荐
Pytest series-01-installation and introduction
静态路由的原理和配置
3、 Experimental report on the implementation of SMB sharing and FTP construction by freenas
CentOS 7X 安装Mysql 数据库
01 knapsack filling form implementation
Linux下安装单机版redis
26 years old, has worked in automation for three years, and the monthly salary is only 12K. Can you change jobs and find a higher salary job?
The experience of finding a job after coming out of the software testing training class taught me these five things
It's so delicious. I finally understand why so many people want to switch to software testing~
C# .Net Core and Net5 Skills record
Number system conversion and subnet Division
CCF 202012-2 期末预测之最佳阈值
LVM and disk quota
LVM与磁盘配额
OSPF实验
Set、Map、WeakSet 和 WeakMap 的区别
DIY a cache
关于pycharm汉化 2020-09-20
V891-Z3735F重做系统,驱动修复汇总
Detailed explanation of sliding window