为什么我的 $http.post return 出现 400 错误?
Why does my $http.post return a 400 error?
我对 MEAN 还很陌生,如果这个问题如此明显,我深表歉意。我想在联系人单击发送按钮时向他们发送电子邮件。我处理发送电子邮件的代码使用 post 我目前使用 SendGrid Nodejs API 发送电子邮件。问题是我将 运行 保持为 400 Post 错误。
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);
});
});
目前电子邮件是硬编码的,但我会在解决 post 问题后进行更改。如果您能指出正确的方向,那将非常有帮助。谢谢。
看起来您希望请求正文包含 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);
使用此代码
$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());
我对 MEAN 还很陌生,如果这个问题如此明显,我深表歉意。我想在联系人单击发送按钮时向他们发送电子邮件。我处理发送电子邮件的代码使用 post 我目前使用 SendGrid Nodejs API 发送电子邮件。问题是我将 运行 保持为 400 Post 错误。
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);
});
});
目前电子邮件是硬编码的,但我会在解决 post 问题后进行更改。如果您能指出正确的方向,那将非常有帮助。谢谢。
看起来您希望请求正文包含 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);
使用此代码
$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());