前端ajax进入nodejs,nodejs如何发送ajax到某台服务器? 代码如下,搞不清为什么app.get是可以的,app.post没有反应。http.request 里面都没有执行。
//routes.js
var sign = require('/sign');
//这个是 OK的
app.get('/api/getUser', function(req,res){
sign.find(req,function(res,data){
res.render('ruleConfig.html',{user:data});
})
})
//这个没反应
app.post('/api/getUser', function(req,res){
sign.find(req,function(res,data){
res.contentType('json');
res.write(JSON.stringify(data));
res.end();
})
})
//sign.js
var http = require('http');
exports.find = function(req,success){
var headers = req.headers;
headers.host = 'www.xxx.com';
var options = {
host: 'www.xxx.com',
port: 80,
path: '/getUser',
method: 'GET',
headers: headers
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
var data = JSON.parse(data);
success(res,data);
});
});
req.on('error', function(e){
console.log("auth_user error: " + e.message);
});
req.end();
}