AJAX将数据发送到Node.js服务器 [英] AJAX sending data to Node.js server

查看:118
本文介绍了AJAX将数据发送到Node.js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AJAX将数据发送到Node.js服务器,但仍然遇到同样的问题,接收。

I'm trying to send data with AJAX to a Node.js server but keep bumping into the same issue, the reception.

这是客户端JavaScript / AJAX代码:

Here is the client-side JavaScript / AJAX code :

    var objects = [
        function() { return new XMLHttpRequest() },
        function() { return new ActiveXObject("MSxml2.XMLHHTP") },
        function() { return new ActiveXObject("MSxml3.XMLHHTP") },
        function() { return new ActiveXObject("Microsoft.XMLHTTP") }
    ];
    function XHRobject() {
        var xhr = false;
        for(var i=0; i < objects.length; i++) {
            try {
                xhr = objects[i]();
            }
            catch(e) {
                continue;
            }
            break;
        }
        return xhr;
    }
    var xhr = XHRobject();
    xhr.open("POST", "127.0.0.1:8080", true);
    xhr.setRequestHeader("Content-Type", "text/csv");
    xhr.send(myText);
    console.log(myText);

出于某种原因,您可以使用基本的HTTP服务器 http://nodejs.org/api/http.html 导致 ECONNREFUSED 错误(即使使用sudo和端口8080)所以我尝试使用这个简单的代码:

For some reason the basic HTTP server you can get on http://nodejs.org/api/http.html was causing ECONNREFUSED errors (even with sudo and port 8080) so I tried with this simple code :

var http = require('http');

http.createServer(function(req, res) {
    console.log('res: ' + JSON.stringify(res.body));
}).listen(8080, null)

但是它一直打印 res:undefined

另一方面, AJAX POST 似乎没有在控制台中触发任何错误。

On the other hand, the AJAX POST doesn't seem to trigger any errors in the console.

所以我的问题如下:


  • 我怎样才能拥有一个简单但可靠的node.js服务器,它可以检索由 AJAX <发送的
    文本code> POST 请求?

  • How can I have a simple but solid node.js server that can retrieve text that was send by an AJAX POST request ?

提前谢谢!

编辑:测试在 127.0.0.1 (localhost)上完成。

Edit : The testing is done on 127.0.0.1 (localhost).

EDIT2:这是更新的Node.js代码:

This is the updated Node.js code :

var http = require('http');

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end();
      req.on('data', function(data) {
        console.log(data);
      })
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');


推荐答案

查看 http://nodejs.org 主页。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');




  1. Node.js请求对象没有以纯文本形式请求正文。它需要由来自数据的块 事件处理

  2. 您需要通过res.end结束请求才能向客户发送回复。

更新:

此代码应该可以正常工作:

This code should work fine:

var http = require('http');
http.createServer(function (req, res) {
  var body = '';
  req.on('data', function(chunk) {
    body += chunk.toString('utf8');
  });
  req.on('end', function() {
    console.log(body);
    res.end();    
  });
}).listen(8080);

这篇关于AJAX将数据发送到Node.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆