Node.js/cors:修订间差异
来自康健生活
< Node.js
创建页面,内容为“== 引言 == == 后端 == <blockquote><pre> npm install cors --save</pre></blockquote> <syntaxhighlight lang="javascript"> var cors = require('cors'); app.use(cors(…” |
|||
| (未显示同一用户的2个中间版本) | |||
| 第11行: | 第11行: | ||
alloweHeaders:['Conten-Type', 'Authorization'] | alloweHeaders:['Conten-Type', 'Authorization'] | ||
}));</syntaxhighlight> | }));</syntaxhighlight> | ||
== error == | |||
* header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute | |||
: <syntaxhighlight lang="javascript" highlight="4"> | |||
server.use( | |||
cors({ | |||
origin: ['http://localhost:8080', '*', 'http://localhost:3000'], //设置原始地址 | |||
credentials: false, //允许携带cookie | |||
methods: ['GET','POST'], //跨域允许的请求方式 | |||
// allowedHeaders: 'Content-Type,Authorization,xc-auth,xc-gui', //允许请求头的携带信息 | |||
exposedHeaders: 'xc-db-response' | |||
}) | |||
);</syntaxhighlight> | |||
* CORS 错误: PreflightMissingAllowOriginHeader | |||
: 错误复现方式是,客户端与服务端不同域,由服务端跳转到第三方网站,显示 <code>CORS 错误: PreflightMissingAllowOriginHeader</code>,请求头丢失。 | |||
2022年1月17日 (一) 05:54的最新版本
引言
后端
npm install cors --save
var cors = require('cors');
app.use(cors({
origin:['http://localhost:8080'],
methods:['GET','POST'],
alloweHeaders:['Conten-Type', 'Authorization']
}));
error
- header in the response is which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute
server.use( cors({ origin: ['http://localhost:8080', '*', 'http://localhost:3000'], //设置原始地址 credentials: false, //允许携带cookie methods: ['GET','POST'], //跨域允许的请求方式 // allowedHeaders: 'Content-Type,Authorization,xc-auth,xc-gui', //允许请求头的携带信息 exposedHeaders: 'xc-db-response' }) );
- CORS 错误: PreflightMissingAllowOriginHeader
- 错误复现方式是,客户端与服务端不同域,由服务端跳转到第三方网站,显示
CORS 错误: PreflightMissingAllowOriginHeader,请求头丢失。
No categories