【问题标题】:Why does my $http.post return a 400 error?为什么我的 $http.post 返回 400 错误?
【发布时间】:2016-08-31 14:02:25
【问题描述】:

我对 MEAN 相当陌生,如果这个问题如此明显,我很抱歉。我想在联系人单击发送按钮时向他们发送电子邮件。我处理发送电子邮件的代码正在使用我目前正在使用 SendGrid Nodejs API 发送电子邮件的帖子。问题是我一直遇到 400 Post Error。

This is the error I get in my Google Chrome Console

This is the error I get in my server terminal

这是在我的 controller.js 中:

$scope.send = function(contact) {
    console.log("Controller: Sending message to:"+ contact.email);
    $http.post('/email', contact.email).then(function (response) {
          //  return response;
          refresh();
        });
    };

此代码在我的 server.js 中:

var express = require("express");
var app = express();
//require the mongojs mondule
var mongojs =  require('mongojs');
//which db and collection we will be using
var db = mongojs('contactlist', ['contactlist']);
//sendgrid with my API Key
var sendgrid = require("sendgrid")("APIKEY");
var email = new sendgrid.Email();
var bodyParser = require('body-parser');
//location of your styles, html, etc
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
    app.post('/email', function (req, res) {
         var curEmail = req.body;
          console.log("Hey I am going to send this person a message:" + curEmail);
      var payload   = {
        to      : 'test@gmail.com',
        from    : 'test1@gmail.com',
        subject : 'Test Email',
        text    : 'This is my first email through SendGrid'
      }
      sendgrid.send(payload, function(err, json) {
      if (err) {
        console.error(err);
      }
      console.log(json);
      });
    });

目前电子邮件是硬编码的,但我会在解决帖子问题后进行更改。如果您能指出我正确的方向,那将非常有帮助。谢谢。

【问题讨论】:

  • 好像你没有回复$http.post('/email', contact.email)
  • 检查您的 POST 请求中的请求标头。当您尝试读取 Content-Type: application/x-www-form-urlencoded as a Content-Type: application/json 时,您遇到的错误很常见
  • 当您在 $http.post('/email', contact.email) 中提供您的数据时...您确定 contact.email 是像 { email: 'test@email. com'}。根据错误,问题出在您的请求格式(客户端格式错误)。
  • 我相信contact.email是正确的,因为在我把图片打印到谷歌浏览器控制台中,这是正确的电子邮件
  • 似乎服务器的数据是 json,但是由于您将电子邮件地址作为第一个字符为 t 的字符串发送(根据错误),它不喜欢它。只需尝试用JSON.stringify({email: contact.email}}; 替换contact.email 即可。

标签: javascript angularjs node.js express mean-stack


【解决方案1】:

看起来您希望请求正文包含 JSON,使用以下行:

app.use(bodyParser.json());

您的控制台中的错误为Unexpected token,这让我相信body-parser 遇到了它无法解析为JSON 的东西......可能是一个字符串。这意味着您将电子邮件作为请求正文中的字符串发送。

简单的解决方法是更改​​您在客户端发送请求的方式:

var data = { email: 'some@email.com' }; // as opposed to just 'some@email.com'

$http.post('/email', data).then(refresh);

【讨论】:

  • bodyparser.urlencoded() 代替bodyparser.json() 也可以吗?
  • @GibryonBhojraj 是的!简短的回答是,用于解析正文的方法需要匹配正文的编码
  • 谢谢你,它真的很管用!我对使用 MEAN 进行开发非常陌生,我盯着那段代码看了好几个小时,而你却能够如此轻松地解决它。你有什么调试技巧吗?
  • @WilliamPring 很高兴我能提供帮助。在这种特定情况下,您只需阅读 ExpressJS 文档并了解您正在使用哪些工具(例如,正文解析器及其工作原理)。
【解决方案2】:

使用此代码

$scope.send = function(contact) {
    console.log("Controller: Sending message to:"+ contact.email);
    $http.post('/email', contact).then(function (response) {
          //  return response;
          refresh();
        });
    };

在服务器端

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser());

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多