在以下示例中,我尝试将数据从javascript客户端发送到Express节点服务器,并验证数据
针对服务器上的数据库,并在验证完成后响应。 newHttpRequest.send(data)中的数据对象
包含JSON格式的配对值。
在下面的示例中,POST命令使用指定的路径将数据对象发送到服务器。这很好。什么
它不会将响应发送回客户端以验证数据。当我运行代码示例时,就绪状态永远不会
超过1(建立服务器连接)。如果我将POST参数更改为GET,则就绪状态从1变为2到3
可以将其设置为4,这与基于路径设置为任何内容的responseText值一样。 GET命令的问题是
数据永远不会从客户端发送或服务器接收。
我的问题是,我是否既可以将数据发布到节点服务器,又不能从中获取一个responseText来说明数据已成功完成,
已验证?
{ // 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 服务器并从中获取responseText?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36956002/