因为项目须要,我用Node写的后台须要处理http跨域请求,解决方式以下:跨域
app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/ else next(); });
其实原理就是在返回头里加入容许跨域访问的返回头参数,app
1是Access-Control-Allow-Origin 容许的域
2是Access-Control-Allow-Headers 容许的header类型
3是Access-Control-Allow-Methods 容许的请求方法code
这三项均可以设置为"*"表示接受任意类型的请求it