Ich ziemlich neu zu durchschnittlich, so tut mir leid, wenn diese Frage so offensichtlich ist. Ich möchte eine E-Mail an einen Kontakt senden, wenn sie auf eine Senden-Schaltfläche klicken. Mein Code für die Bearbeitung einer Sende-E-Mail verwendet einen Post, mit dem ich derzeit eine SendGrid Nodejs-API zum Senden der E-Mail verwende. Das Problem ist, dass ich in einen 400 Post Error renne.
This is the error I get in my Google Chrome Console
This is the error I get in my server terminal
Dies ist in meinem 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();
});
};
dieser Code in meinem server.js ist:
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 : '[email protected]',
from : '[email protected]',
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);
});
});
Derzeit ist die E-Mail ist hart codiert, aber ich werde die Änderung vornehmen, nachdem ich die Post iss behoben habe Ue. Wenn Sie mir in die richtige Richtung zeigen könnten, wäre das sehr hilfreich. Vielen Dank.
scheinen als hätten Sie nicht die '$ http.post ('/ email', contact.email)' – MarkoCen
beantwortet Überprüfen Sie die Anfrage-Header von Ihrer POST-Anfrage. Der Fehler, den Sie bekommen, kommt häufig vor, wenn Sie versuchen, einen Inhaltstyp zu lesen: application/x-www-form-urlencoded als Inhaltstyp: application/json – ruedamanuel
Wenn Sie Ihre Daten in $ http.post ('/ email', contact.email) ... sind Sie sicher, dass contact.email ein Objekt wie {email: '[email protected]}} ist. Nach dem Fehler ist das Problem mit dem Format Ihrer Anfrage (clientseitige Fehlbildung). – Anfelipe