通过XMLHttpRequest,我用
发送一些数据 const xhr = new XMLHttpRequest()
xhr.onreadystatechange = function (event) {
if (xhr.readyState === 4 /** responseText is not available yet */) {
const statusCode = xhr.status
const responseText = xhr.responseText
// some callback functions
}
}
xhr.open('POST', url, true /** async */)
xhr.setRequestHeader('Content-Type', 'text/plain')
xhr.withCredentials = true
xhr.timeout = timeoutMillis
xhr.ontimeout = function (event) {
// some time out error log
}
xhr.send(JSON.stringify(payloads))
和服务器端node.js服务器代码是
var httpPort = 3001
var http = require('http')
,qs = require('querystring');
http.createServer(function (req, res) {
console.log(req.url + " " + req.method);
if( req.url == '/mytoken' && req.method == 'POST'){
console.log('start');
var body = [];
res.writeHead(200, {'Content-Type':'text/plain'});
req.on('data', function(chunk){
body.push(chunk);
console.log('GOT DATA: ' + body);
});
res.end('OK');
}else{
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404 ERROR');
}
}).listen(httpPort);
我写了这样的服务器代码。但是,服务器没有发送任何响应。
应该是statusCode 200 with responseText' OK'。
我错过了一些?或做错了吗?
编辑1:编辑服务器代码并读取发送给我的数据