在下面的示例中,我尝试将数据从 javascript 客户端发送到 Express Node 服务器,验证数据 针对服务器上的数据库并在验证完成后做出响应。 newHttpRequest.send(data) 中的数据对象 包含 JSON 格式的配对值。
在下面的示例中,POST 命令使用指定的路径将数据对象发送到服务器。这效果很好。什么 它不做的是将响应发送回客户端来验证数据。当我运行代码示例时,就绪状态永远不会 超过 1(已建立服务器连接)。如果我将 POST 参数更改为 GET,则就绪状态将从 1 进展到 2 到 3 为 4,正如您所期望的,responseText 值基于路径设置的值。 GET 命令的问题是 数据永远不会从客户端发送或由服务器接收。
我的问题是,我是否可以同时将数据发布到 Node 服务器并从中获取响应文本,表明数据已成功 已验证?
{ // XMLHttpRequest
var newHttpRequest = new XMLHttpRequest();
var path = "http://00.0.0.00:80";
newHttpRequest.onreadystatechange = function()
{
alert("onreadystatechange: " + newHttpRequest.readyState );
if ( newHttpRequest.readyState === 4 && newHttpRequest.status === 200)
{
var myResponseText = newHttpRequest.responseText;
alert ("responseText: " + myResponseText);
}
};
newHttpRequest.open("POST", path, true);
newHttpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
newHttpRequest.setRequestHeader("header_nb", "1");
newHttpRequest.setRequestHeader("header_ds", "logon");
newHttpRequest.setRequestHeader( "Content-Type", "application/json; charset=UTF-8" );
newHttpRequest.send(data);
} // eof code block
最佳答案
您一次可以发送 POST 或 GET 请求。您可以在发送 POST 后从服务器获取(接收)数据,也可以在发送 POST 后发送 GET 来接收其他一些数据;这只是语义:)
这是一个例子,希望对您有所帮助:
前端(index.html +您修改后的脚本):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
{ // XMLHttpRequest
var newHttpRequest = new XMLHttpRequest();
var path = "http://0.0.0.0:3000";
newHttpRequest.onreadystatechange = function()
{
alert("onreadystatechange: " + newHttpRequest.readyState );
if ( newHttpRequest.readyState === 4 && newHttpRequest.status === 418)
{
var myResponseText = newHttpRequest.responseText;
alert ("responseText: " + myResponseText);
}
};
var data = {
hero: 'Spiderman Spiderman',
ability: 'He can open a tuna can'
};
newHttpRequest.open("POST", path + '/my-post-route', true);
newHttpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
newHttpRequest.setRequestHeader("header_nb", "1");
newHttpRequest.setRequestHeader("header_ds", "logon");
newHttpRequest.setRequestHeader( "Content-Type", "application/json; charset=UTF-8" );
newHttpRequest.send(JSON.stringify(data));
}
</script>
</body>
</html>
后端(index.js、node+express):
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = express();
// Configure express settings; standard stuff; research what they do if you don't know
app.set('trust proxy', true);
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
app.use(bodyParser.json({limit: '10mb'}));
app.use(cookieParser());
// Configure routes
app.post('/my-post-route', function (req, res, next) {
// ... do what you need to do here ...
console.log(req.headers); // to see what those headers contain
console.log(req.body); // Look ma! It's Spiderman!
var ImATeaPot = true;
if (ImATeaPot)
return res.status(418).send("I'm a teapot!"); // return to end here (if you are a teapot) and send a string; you can chain status, send and other commands
res.send("I'm not a teapot! :O"); // Oh yes you are!
// send status is 200 by default, so you don't need to set it if that's what you need
// res.json({ myText: "Hello World!" }); // you can send an object with .json(); also status 200 by default
// res.status(500).end(); // you can just send a status and no body; always remember to send something or end it or next() if you want to keep going with some other express code
});
// for testing purposes we send the index.html
app.get('/*', (req, res) => res.sendFile(__dirname + '/index.html'));
// Start the server; Listen for requests on the desired port
var server = app.listen(3000, function () {
return console.log('Hello World!');
});
module.exports = server;
package.json
{
"dependencies": {
"body-parser": "^1.14.1",
"cookie-parser": "^1.4.0",
"express": "^4.12.2"
}
}
在终端中:
npm install
node index.js
在浏览器中,转到 0.0.0.0:3000
关于javascript - 如何将数据发布到 Node 服务器并从中获取响应文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36956002/