当前位置:网站首页>Nodejs cross domain CORS
Nodejs cross domain CORS
2022-07-19 01:32:00 【venture to ask】
CORS Introduce
CORS It's a W3C standard , The full name is " Cross-domain resource sharing "(Cross-origin resource sharing).
It allows the browser to cross to the source server , issue XMLHttpRequest request , To overcome AJAX A restriction that can only be used with the same origin .
CORS Requires both browser and server support . at present , All browsers support this feature ,IE The browser cannot be lower than IE10.
Whole CORS Communication process , It's all done automatically by the browser , No user involvement is required . For developers ,CORS Correspondence and homology AJAX There is no difference in communication , The code is exactly the same . Once the browser finds AJAX Request cross source , Will automatically add some additional header information , Sometimes there is an additional request , But users don't feel .
therefore , Realization CORS The key to communication is the server . As long as the server implements it CORS Interface , You can communicate across sources .
NodeJS Realization CORS Cross domain
stay phpnow In the environment (phpnow Default port 80) towards node( port 3000) Send a request
phpnow Request code in (index.html)
<script>
$("button").click(function () {
$.ajax({
"url":'http://127.0.0.1:3000',
"success": function(data){
console.log(data,'data')
}
})
})
</script>NodeJS The code in app.js
var express = require("express")
var app = express()
app.get("/",function (req,res) {
// Configure the requested domain name ,* For any
res.header("Access-Control-Allow-Origin", "*");
// Support cross domain request types
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
// Configure request header information , Including character set, etc
res.header("Content-Type", "application/json;charset=utf-8");
res.json({
a:100
})
})
app.listen(3000,function () {
console.log(" monitor 3000")
})
边栏推荐
- JS intercepts the first few digits of the string or the last few digits of the string
- P6-day01-general attempt (P4 review)
- Day05-Cookie,Session,Csrf
- uni canvas截取图片
- 微信推送-模版消息参数配置
- Buuctf [bjdctf2020]ezphp1 detailed explanation
- 【ElenmentUI el-date-picker日期选择器,结束时间不得早于开始时间,且只能选择距开始时间指定天数的日期】
- (10)文件包含
- 今天的码农女孩总结了jQuery处理缓存的方法和事件委托方法的区别的笔记
- 今天的码农女孩学习了关于事件操作和ref属性的笔记并做了表单双向绑定的练习
猜你喜欢

uni-app微信小程序——商城(3)——商城主页

NodeJS 跨域 CORS

二分法查找

Use leaflet to copy the original shentiwa Mega map to make a diary

Use bat to automatically execute CMD commands (multiple commands or a single command)

gtest与gmock的安装与使用

cmake的基本使用

Day06 ORM field and operation

ModelArts-人声检测and文本分类

If the website is hacked, what if you jump to other websites through Baidu / Sogou and other search keywords?
随机推荐
win10解压文件时,出错:不能创建符号链接 你可能需要以管理员权限运行Winrar 客户端没有所需的特权
elemtnui 表格如何修改某行文字颜色(elemtnui table 修改某行文字颜色)
JS get the suffix format of a file name
数据库连接池、sequelize实现增删改查等操作
Uniapp calls the map to query the location and mark the location
(五)printf(代替echo)
tp-watermark.js网页添加水印插件
当 std::bind 遇上 this
maker-论文资料查找培训-笔记
JS string to object JS object to string JS string to object
Day05-Cookie,Session,Csrf
cmake的基本使用
Uni app wechat applet - Mall (7) - Product Details
Raw Socket抓包,某些端口的数据包抓不到
If the website is hacked, what if you jump to other websites through Baidu / Sogou and other search keywords?
sql语句学习和pymysql的使用
Eye of depth III - (7, 8)] mathematics: matrix diagonalization and quadratic form 2.3
(三)传递参数
(四)运算符
The C Programing Language--(2nd)--笔记--4.11.2