0
我试图在节点js服务器上提供XMLHttprequest(POST)。在节点js服务器上提供XMLHttp POST请求
服务器端:
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.post('/count', function(request, response){
console.log(request.body);
response.send(request.body);
});
客户端:
<script>
var xmlhttp = new XMLHttpRequest();
var resp = ""
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
resp = xmlhttp.responseText;
resp = JSON.parse(resp);
document.write(resp);
}
}
var check = "Hello";
function show(){
xmlhttp.open("POST","http://localhost:3000/count", true);
xmlhttp.send(JSON.stringify(check));
</script>
输出即时得到为[对象对象]
我在哪里去了?
尝试'JSON.stringify(request.body).forEach(console.log)'并查看它打印的内容。 如果您在客户端使用'fetch' API,可能会使您的代码更易于理解:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch – SamHH
没有理由保持字符串化。我不知道人们从哪里得到这些信息。 –
对于初学者,用'console.log(resp)'替换'document.write(resp)'。你在浏览器的控制台中看到了什么? –