nodejs+ajax如何处理ajax的跨域问题
 发布于 9 年前  作者 lhist  9100 次预览  最后一次回复是 9 年前  来自 问答 

nodejs在不用框架的情况下如何设置服务器头部 类似php的header( “Access-Control-Allow-Origin:*” );header( “Access-Control-Allow-Methods:POST,GET” );这两句代码
就是在nodejs的服务器端如何解决ajax的跨域问题。

7 回复
lhist

各位大神帮忙看看

yxy19950717

最近刚好做了node的CORS跨域,其实也就类似php几行代码。 res.header(“Access-Control-Allow-Origin”,"*"); res.send(‘这是跨域的数据!’);

lhist

@yxy19950717 为什么报错 res.header is not 啊function

lhist

@gjc9620 写在 var server = http.createServer(function(req, res){ }).listen…里

yxy19950717

@lhist 你查一下http模块的API,我用的是express

leungwensen

这种感觉还是用库好一点,CORS标准里细节蛮多的,除非想 深入了解CORS,否则用现成的、经过验证的模块好一点。譬如这个: cors