【问题标题】:Why is an object in an XMLHttpRequest sent to a Node/Express server empty?为什么发送到 Node/Express 服务器的 XMLHttpRequest 中的对象是空的?
【发布时间】:2015-11-12 02:35:20
【问题描述】:

我正在尝试制作一个表格,该表格采用电子邮件地址并将交易电子邮件发回。我在 vanilla JavaScript 中使用 XMLHttpRequest 向服务器发送数据,但是当我查看从 index.html 发送的数据时,它只是服务器端的一个空对象。

在后端,我使用的是 Node 和 Express 以及 Nodemailer。 Nodemailer 工作正常。我一直在试图弄清楚为什么查询对象中没有任何内容。

// Here is server.js

var express = require('express');
var nodemailer = require('nodemailer');
var app = express();

// Send index.html
app.get('/', function(request, response) {
  response.sendfile('index.html');
});

// Where I should receive data from JS written in index.html
app.post('/send', function(req, res) {
  var mailOptions  =   {
    to: req.query.to,
    subject: req.query.subject,
    text: req.query.text
  }
});
<!-- Here is my index.html with some JS in it -->

<div>
  <input id="to" type="text" placeholder="Email" />
  <input id="subject" type="text" placeholder="subject" />
  <textarea id="content" cols="20" rows="2" placeholder="Write something"></textarea>
  <button id="submit">Submit</button>
</div>

<script>
  // When #submit is clicked it invokes a function to collect values and then makes a XMLHttpRequest like bellow
  data = {to: to, subject: subject, text: text};
  var request = new XMLHttpRequest();
  request.open('GET', 'http://localhost:3000/send', true);
  request.send(data);
  }
</script>

【问题讨论】:

  • 您的 XMLHttpRequest 发送了一个“GET”请求,但您的快速服务器期待一个“POST”请求
  • @PHPglue 他提出的代码是他实际代码的 sn-p 因此为什么它是未定义的
  • 没有使用.send() 发送GET,只使用.open() URL。如果要发送数据,请使用 POST 或将数据附加到 URL,例如:?to=val1&amp;subject=val2&amp;text=val3。要么使用jQuery,然后你就可以传递那个数据对象。
  • 真的很抱歉这个错误。我不小心离开了 .post 而不是 .get,因为我在不同的情况下尝试了这两对。

标签: javascript node.js express xmlhttprequest


【解决方案1】:

在此之前的一些事情可以工作

  • 决定是使用 GET 还是 POST,您似乎对使用哪一个感到困惑。我会使用 POST,因为您正在尝试为电子邮件发送数据,而不是真正尝试从服务器获取数据。
  • 更改您的 app.post 节点功能(假设您要发布)
  • 您需要向服务器发送一个字符串,因此需要 json 字符串化
  • 由于您的字符串是 json 格式,您需要将标题“Content-Type”更改为“application/json”
  • 您需要将请求动词更改为“POST”以匹配您的服务器以及您要完成的任务

在您的服务器中,您需要将 app.post 代码替换为(您需要 npm install body-parser)

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// Where I should receive data from JS written in index.html
app.post('/send', function(req, res) {
  var mailOptions  =   {
    to: req.body.to,
    subject: req.body.subject,
    text: req.body.text
  }
});

这应该可以在客户端上解决问题

data = {to: to, subject: subject, text: text};
var request = new XMLHttpRequest();
request.open('POST', 'http://localhost:3000/send', true);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send(JSON.stringify(data));

XMLHttpRequest 的替代解决方案

或者,您可以通过 HTTP api 查看这个库以获取糖 - axios

如果你用的是axios,那么简单

data = {to: to, subject: subject, text: text};
axios.post('/user', data);

或者如果您想控制收到回复时发生的情况。

data = {to: to, subject: subject, text: text};
axios.post('/user', data)
  .then(function (response) {
    console.log('success');
  })
  .catch(function (response) {
    console.log('error');
  });

【讨论】:

  • 我刚刚实施了更改并且工作得很好。我也很欣赏替代解决方案。我也会尝试一下。这简直是​​完美的答案!
  • 不用担心@jayscript 你介意勾选解决方案并投票吗?
  • 这是我的第一个 Stack Overflow 问题。一旦我积累了 15 名声望,我将支持你的答案!我只需要5个......
  • “勾选解决方案”是什么意思?有没有办法表明我找到了解决方案?
  • @YangLi - 这是一个很好的答案。感谢分享!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 2019-10-24
  • 2013-11-30
  • 2015-05-16
  • 2020-03-01
  • 2018-05-26
相关资源
最近更新 更多