Home  >  Article  >  Web Front-end  >  The perfect solution to NODE.JS cross-domain problems

The perfect solution to NODE.JS cross-domain problems

高洛峰
高洛峰Original
2017-02-04 10:25:441428browse

In the past few days, when a company colleague (front-end) wrote a page, he kept saying that he couldn't get the JSON he wanted. It is available on Android and iOS, but he is also a newbie and doesn't know why. He only knows that it is a js cross-domain problem. , and then asked me that I didn’t understand the front-end, so I started Baidu.

Some people said that Google Chrome needs to be set up cross-domain, and then I added a --disable-web-security after the target of Google Chrome.

The perfect solution to NODE.JS cross-domain problems

#But later I found that an error was still reported and the desired data was still not available. Later, I kept looking around and found nothing.

Until today, I was inspired by the cross-domain problem of PHP on Baidu, so Baidu found the cross-domain problem of node.js. Finally, I added a cross-domain code in the app.js routing settings to perfectly solve the problem:

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();
});
//app.listen(8088);

The above is the perfect solution to the NODE.JS cross-domain problem introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. , the editor will reply to everyone in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to the perfect solution to NODE.JS cross-domain problems, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn