[英]About node.js return data to ajax call function
現在我有一個這樣的問題:我創建了一個帶有node.js的服務器,並且服務器已經收到了一個ajax請求。利用從ajax收到的數據,node.js向另一個服務器發送一個post請求。 現在我從另一台服務器獲得了數據,主要問題是如何將數據發送回ajax,我嘗試了很多方法,但它不起作用。
有人可以幫我解決這個問題嗎?
這是我的代碼
==== ajax請求
$.ajax({
type: "POST",
url: 'http://localhost:8888', // 這里要改成服務器的地址
data: userData,
success: function (data) {
console.log(data);
}
})
====
http.createServer(function (req, res) {
if (req.url == '/') {
var data = '';
var imdata;
util.log(util.inspect(req));
util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url);
req.on('data', function (chunk) {
imdata = querystring.parse(data += chunk);//轉成對象的格式
})
req.on('end', function () {
var myIm = new ServerApi('e782429e48cb99f44b9c5effe414ac72', 'b88b9f2a2f74');
myIm.createUserId(imdata, function (err, data) {
//createUesrId is a api to deal with post request
console.log(data);//the data have received from another server,and now i do not know how to return the data to ajax success function
})
})
====使用post requeset創建用戶ID的api
ServerApi.prototype.postDataHttps = function (url, data, callback) {
this.checkSumBuilder();
var urlObj = urlParser.parse(url);
var httpHeader = {
'AppKey': this.AppKey,
'Nonce': this.Nonce,
'CurTime': this.CurTime,
'CheckSum': this.CheckSum,
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Content-Length': Buffer.byteLength(data)
};
var options = {
hostname: urlObj.hostname,
port: 80,
path: urlObj.path,
method: 'POST',
headers: httpHeader
};
var that = this;
var req = http.request(options, function (res) {
res.setEncoding('utf8');
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function (chunk) {
if (Object.prototype.toString.call(callback) === '[object Function]') {
var result = JSON.parse(chunk);
callback.call(that, null, result);
return result;
}
});
});
var postData = querystring.stringify(data);
req.write(postData);
req.end(data);
req.on('error', function (err) {
if (Object.prototype.toString.call(callback) === '[object Function]') {
callback.call(that, err, null);
}
});
}
ServerApi.prototype.createUserId = function (data, callback) {
var url = 'https://api.netease.im/nimserver/user/create.action';
var postData = {
'accid': data['accid'] || '',
'name': data['name'] || '',
'props': data['props'] || '',
'icon': data['icon'] || '',
'token': data['token'] || ''
};
this.postDataHttps(url, postData, callback);
}
在您的服務器代碼上。 http.createServer(function (req, res) {...}
的一個http.createServer(function (req, res) {...}
注意你是如何獲得req
和res
參數的?
所以在事件end
,那就是req.on('end' function...
就在您收到評論說“從另一台服務器收到數據”的行之后,你可以做類似的事情;
res.writeHead(/*HTTP_RESPONSE_CODE_FOR_AJAX_CLIENT=*/200);
res.end('Done');
使用HTTP響應代碼= 200將響應發送回客戶端,HTTP主體中的消息將為“完成”。 請注意,您可以使用響應對象執行許多操作,您可能需要查看文檔以獲取更多信息。
看到:
https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
或(中文版)
http://nodeapi.ucdok.com/api/http.html#http_class_http_serverresponse_7847
中文快速解釋:
在服務器的代碼req.on('end'...
,你可以用res
object打回給你的AJax客戶端。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.