[英]Wrong data from POST request in NodeJS
這是我在nodejs中的簡單服務器應用程序。 我的網站上的句柄帖子有問題。
1個帖子=應用未返回任何內容
2個帖子=前一個帖子的應用返回數據
var http = require('http');
var util = require('util');
var tenitem = [];
var dataa;
http.createServer(function (req, res) {
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' });
res.end('Hello World\n');
}
if (req.method === 'POST') {
var body = ''
console.log("POST");
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log(body)
dataa = body;
http.get('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + dataa, function (api) {
var bod = '';
api.on('data', function (chunk) {
bod += chunk;
});
api.on('end', function () {
console.log(bod)
tenitem = JSON.parse(bod);
})
})
});
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.end(tenitem.lowest_price)
}
}).listen(port, hostname, function () {
console.log('Server running');
});
tenitem
在內部函數中初始化,您正在嘗試將其發送回。 只需在該函數中移動res.end()
:
api.on('end', function () {
console.log(bod)
tenitem = JSON.parse(bod);
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.end(tenitem.lowest_price)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.