Comments (1)
跨域解决方案
就说说自己用到过的两个方案吧,其他的就不说了。总(chao)结(xi)一下
- 通过jsonp跨域
jsonp跨域的原理
就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的。
当需要通讯时,本站脚本创建一个<script>元素,地址指向第三方的API网址,形如: <script src="http://www.example.net/api?param1=1¶m2=2"></script> 并提供一个回调函数来接收数据(函数名可约定,或通过地址参数传递)。
第三方产生的响应为json数据的包装(故称之为jsonp,即json padding),形如: callback({"name":"hax","gender":"Male"}) 这样浏览器会调用callback函数,并传递解析后json对象作为参数。本站脚本可在callback函数里处理所传入的数据。
<script>
var script = document.createElement('script');
script.type = 'text/javascript';
// 传参并指定回调执行函数为onBack
script.src = 'http://www.domain2.com:8080/login?user=admin&callback=onBack';
document.head.appendChild(script);
// 回调执行函数
function onBack(res) {
alert(JSON.stringify(res));
}
</script>
- 跨域资源共享(CORS)(Cross-origin resource sharing)
普通跨域请求:只服务端设置Access-Control-Allow-Origin即可,前端无须设置,若要带cookie请求:前后端都需要设置
注:更多 跨域解决方案
from myblog.
Related Issues (20)
- 开发自己的cli工具 HOT 1
- 常用的ES6、ES7、ES8特性 HOT 3
- 在浏览器中输入URL地址到页面显示出来中间发生什么了呢? HOT 3
- 他强由他强,清风拂山岗 HOT 6
- 排序算法
- JavaScript继承 HOT 7
- 工作遇到的配置问题 HOT 2
- js触发子元素事件,而不触发父元素事件(阻止事件冒泡) HOT 2
- HTML input中type='file' Accept的值
- 二叉树心法秘籍
- 初探二叉树
- HTTPS初探 HOT 1
- 基于node的文件上传和下载 HOT 1
- axios拦截器处理重定向
- two sum
- 千分位格式化
- React生命周期和hock 的一些使用心得 HOT 1
- RN的TouchableOpacity的使用笔记
- 微信授权登录解密手机号失败
- moment.js工作中常用的api
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from myblog.