如何将数据发布到节点服务器并从中获取responseText?

问题描述:

在以下示例中,我试图将数据从javascript客户端发送到Express节点服务器,并验证数据 针对服务器上的数据库,并在验证完成后作出响应。 newHttpRequest.send(data) 中的数据对象包含JSON格式的成对值。如何将数据发布到节点服务器并从中获取responseText?

在下面的示例中,POST命令使用指定的路径将数据对象发送到服务器。这很好。 它没有做的是向客户端发送一个响应来验证数据。当我运行代码示例时,就绪状态从未从 获得过去1(建立了服务器连接)。如果我将POST参数更改为GET,就绪状态将从1到2前进到3 到4,如您所期望的那样,根据路径设置为的任何一个responseText值。 GET命令的问题是 ,数据永远不会从客户端发送或由服务器接收。

我的问题是,我不能同时POST数据到节点服务器,并从它GET 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 
+0

什么是您的服务器SUDE代码?你有一个路由处理程序来处理GET请求到http://00.0.0.00:80? –

+0

问题可能出在您的服务器代码上。对于POST请求本身,您可以将数据与响应一起发送。使用express,你可以像res.send(“resText”)那样发送响应。 – raiyan

您可以一次发送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,节点+快递):

'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

+0

谢谢你。正如你所说,你必须要么GET或POST,你不能同时做两个。我想这只有道理。这给了我一些关于如何进行的好主意。欣赏详细的回复。干杯... – Customsoft

+0

嘿,我很高兴它帮助你。如果这一切都很好,你可以接受这个作为你的问题的答案来包装它,否则它保持开放;) – Kesarion

+0

当然,会做,对此感到抱歉。再次感谢... – Customsoft

我在这里看到2个问题。首先,您可能需要在POST调用中添加超时以接收响应。服务器必须响应POST请求,否则您将收到超时错误或内部服务器错误(500)。

其次,您不能在同一个调用中使用POST和GET。当您使用GET而不是POST时,数据变得无关紧要,因为它是一个GET调用,其中数据不会发送到远程端。这就是为什么你很可能接收到GET请求的快速响应。

在我看来,你应该验证远程HTTP服务器将curl命令一次。

+0

Gaurav,您好,感谢您的输入。几点:1)我运行POST命令时没有得到HttpRequest.status === 500。正如我在我的问题中提到的,数据已成功发布到服务器。问题是(和我的问题的基础),我没有得到一个responseText。你是否建议如果我添加超时? 2)是的,我使用GET调用得到了一个快速(以及看起来是正确的)响应,包括我'GET'这个事实看起来是正确的responseText。所以,我不确定这与您的回应是否一致? – Customsoft