解决NodeJS+Express模块的跨域访问控制问题:Access-Control-Allow-Origin
by 董 龙飞
at 2013-04-18 16:06:19
original http://www.donglongfei.com/2013/04/nodejsaccess-control-allow-origin/?utm_source=rss&utm_medium=rss&utm_campaign=nodejsaccess-control-allow-origin
在一个项目上想用NodeJS,所以边学边练。第一个遇到的问题就是跨域访问控制问题。很多初学者会遇到同样问题。 问题 在前端的JS(http://localhost/xxx)中ajax访问后端RestAPI(http://localhost:3000/….)时(Chrome)报错: XMLHttpRequest cannot load http://localhost:3000/auth/xxx/xxx. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 方案 解决代码如下: var express = require('express'); var app = express(); //设置跨域访问 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1') res.header("Content-Type", "application/json;charset=utf-8"); next(); … Continue reading →
The post 解决NodeJS+Express模块的跨域访问控制问题:Access-Control-Allow-Origin appeared first on RIA Coder.
Tags:
Copyright © RIA Coder [解决NodeJS+Express模块的跨域访问控制问题:Access-Control-Allow-Origin], All Right Reserved. 2013.