【问题标题】:Send post request from client to node.js从客户端发送 post 请求到 node.js
【发布时间】:2015-10-25 02:12:11
【问题描述】:

为了学习 node.js,我构建了一个非常简单的留言簿应用程序。基本上只有一个评论表和一个以前的 cmets 列表。目前该应用程序仅在客户端,并且项目存储在本地存储中。

我想要做的是将项目发送到我将使用 Mongo DB 保存它们的节点。

问题是我还没有找到一种方法来建立连接以使用 POST 请求在客户端和 node.js 之间来回发送数据。

在服务器端,我已经为请求添加了监听器并等待数据:

request.addListener('data', function(chunk) {
    console.log("Received POST data chunk '"+ chunk + "'.");
});

在客户端,我使用简单的 AJAX 请求发送数据:

$.ajax({
    url: '/',
    type: 'post',
    dataType: 'json',
    data: 'test'
})

目前这根本不起作用。可能是我不知道在 AJAX 请求“url”参数中放置什么 url。或者整个事情可能只是使用错误的方法进行构建。

我也尝试过实现here描述的方法,但也没有成功。

如果有人能分享一些关于如何使这项工作(从客户端发送 POST 请求到节点并返回)的提示或分享任何好的教程,那将非常有帮助。谢谢。

【问题讨论】:

标签: javascript node.js mongodb post


【解决方案1】:

我刚刚创建了skiliton,你想尝试在客户端和服务器之间建立一个json数据连接,它可以工作,你可以检查下面的代码

服务器端:

var http = require("http");
var url = require("url");
var path = require("path");
var ServerIP = '127.0.0.1',
    port = '8080';

var Server = http.createServer(function (request , response) {
    console.log("Request Recieved" + request.url);
    var SampleJsonData = JSON.stringify([{"ElementName":"ElementValue"}]);
    response.end('_testcb(' + SampleJsonData + ')'); // this is the postbackmethod
   }); 
Server.listen(port, ServerIP, function () {
    console.log("Listening..." + ServerIP + ":" + port);
});

客户端:

jQuery.ajax({
    type: 'GET',
    url: 'http://127.0.0.1:8080/',
    async: false,
    contentType: "text/plain",  // this is the content type sent from client to server
    dataType: "jsonp",
    jsonpCallback: '_testcb',
    cache: false,
    timeout: 5000,
    success: function (data) {
                        
    },
    error: function (jqXHR, textStatus, errorThrown) {
           alert('error ' + textStatus + " " + errorThrown);
    }
});
            
 }
 function _testcb(data) {
//write your code here to loop on json data recieved from server
 }

【讨论】:

    【解决方案2】:

    我强烈推荐 node.js 的 socket.io-modul (http://socket.io/)。 它使建立连接和来回传递数据变得非常容易! 它是事件驱动和非阻塞的。

    【讨论】:

    • 我知道 socket.io,但我只是没有设法让它工作。客户端代码无法读取包含的 socket.io 脚本文件。但也许我会再试一次。
    【解决方案3】:

    您可以使用另一个回调。

    添加这个

    response.addListener('end', function(){
        console.log('done')
        // Now use post data.
    });
    

    阅读帖子数据后。

    我遇到了同样的问题,它成功了

    【讨论】:

      【解决方案4】:

      为此使用 socket.io。 对于客户端和服务器之间的通信,使用 emit() 和 on() 方法。 例如:如果您想从客户端向服务器发送 post 数据,请在客户端发出带有 post 参数的事件。现在在服务器端创建一个事件监听器,它监听客户端发出的相同事件。 在客户端:

          socket=io();
          $('form').submit(function(){
          var param1=$('param1').val();
          ...
          socket.emit('mypost',param1);
          });
      

      在服务器端:

          var io=socket(require('http').Server(require('express')()));
          io.on('connection',function(client){
              client.on('mypost',function(param1){
                  //...code to update database
              });
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-31
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多