javascript - Asynchronous POST shows Unexpected token n -
i posting data via jquery
$.ajax()
call so:
var data = {}; data.name = $("#name").val(); data.email = $("#email").val(); data.message = $("#message").val(); $.ajax({ type: "post", url: "http://myhost/contact/sendemail", data: data, contenttype: "application/json", datatype: "json", success: function (data) { console.log("success"); } });
which getting routed contactroutes.js
:
contactrouter.route("/sendemail").post(contactcontroller.sendemail);
to controller supposed pull body of request make api call , send email message.
sendemail:
var sendemail = function(req, res) { var payload = { to: req.body.email, from: "noreply@test.com", subject: req.body.name, text: req.body.message }; ...omitted brevity };
the error continue receive syntaxerror: unexpected token n
in body-parser
module. have app.js
setup this:
app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true }));
i'm lost look. i've examined json
object , appears correct, i'm missing something. have suggestions? also, how can sure contactroutes.js
picking content passed in data
?
Comments
Post a Comment