The front end obtains the data of World Wide Web Publishing Service service localhost: 3000′ is therefeore not allowedaccess.

  node.js, question

The node.js front-end runs on localhost:3000.
Getting the data returned by java services running in World Wide Web Publishing Service through ajax reported this error:
(local:8080/wechat/api/alarms/1)

XMLHttpRequest cannot loadhttp://localhost:8080/wechat/api/alarms/1.Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3000‘ is therefore not allowed access.

js———————————————————————
var alarmUrl=’http://localhost:8080/wechat/api/alarms‘;
mui.ajax({

Url:alarmUrl plus'/'plus page.
 type:'GET',
 dataType:'json',
 success:function(data){
 console.log(data);
 },
 error:function(xhr,type,errorThrown){
 //Print and Report Errors
 Log (xhr plus' \n' plus type plus' \n' plus errorThrown);
 bracket

java——————————————————————-
Provide data externally through RESTful API interface

Interface uri:
 /wechat/api/alarms/{page}

Node–app.js code-
var express=require(‘express’);
var app=express();
var path=require(‘path’);
var logger = require(‘morgan’); //logger (recorder), in the console, displays the information requested by req.
var http=require(‘http’)
//bodyParser is used to parse the contents of the body requested by the client. JSON encoding, url encoding and file uploading are used internally.
var bodyParser=require(‘body-parser’);
var favicon=require(‘serve-favicon’);
var cookieParser=require(‘cookie-parser’);
var session=require(‘express-session’);

var news_main=require(‘./routes/news_main’);
var pullrefresh_sub=require(‘./routes/pullrefresh_sub’);
var boiler_alarm=require(‘./routes/boiler_alarm’)
var login=require(‘./routes/login’);

app.use(session({

secret:'a~Kf?)P{3490*&(',//密码字符串
maxAge:3600000,//定制session的会话时长
resave:true,//是否每次都保存
saveUninitialized:true

}));

//var news_page=require(‘./routes/news_page’);
// app.get(‘/’,function(req,res){
// res.render(‘news_main’);
// });

//Define Template
app.set(‘views’,path.join(__dirname,’views’));
app.engine(‘html’,require(‘ejs’).renderFile);
app.set(‘view engine’,’html’);
//static file directory,
app.use(express.static(path.join(__dirname,’public’)));

app.use(logger(‘dev’));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({

extended:false

}));

app.use(cookieParser());

/**
* routing configuration
*/
app.use(‘/’,news_main);
app.use(‘/pull’,pullrefresh_sub);
app.use(‘/alarm’,boiler_alarm);
app.use(‘/’,login);

var server=app.listen(3000,function(){

var host=server.address().address;
 var port=server.address().port;
 Log ("exampleboilerlisting at http://percent s: percent s",host,port);

});

Node node—boiler_alarm.js (routing part code)-

var express=require(‘express’);
var router=express.Router();
router.get(‘/’,function(req,res,next){

res.render('boiler_alarm',{
 Title:' Alarm Module'
 });

});

I saw this online, but it didn’t work.
//router.use(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”);
//next();
//});

module.exports=router;

Nodejs to lift cross-domain restrictions:

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();

});