0

environment:

-> Front-end: jquery that randle ajax request

-> Back-end: nodejs, expressjs (v.3.4.8).

I trying to create a simple contact form.

my front-end code:

var nome = $('#nome').val();
var email = $('#email').val();
var assunto = $('#assunto').val();
var mensagem = $('#mensagem').val();

var info = {'nome':nome, 'email':email, 'assunto':assunto, 'mensagem':mensagem};

$.ajax({
  type: "POST",
  url: "/contact",
  data: { info: JSON.stringify(info) }
}).done(function(retorno){
  //do something important
});

this work fine, i tested and on firebug everything from front-end is ok.

but i dont know how to deal with express that need read json from post request.

I just have it:

app.post('/contact', function(req, res){

});
Undefined Behavior
  • 2,128
  • 4
  • 24
  • 34
  • possible duplicate of [how to get POST query in express node.js?](http://stackoverflow.com/questions/5710358/how-to-get-post-query-in-express-node-js) – bredikhin Mar 05 '14 at 18:30
  • but nothing in that question help until now – Undefined Behavior Mar 05 '14 at 19:06
  • It does look like the question @bredikhin posted should answer this. It explains which Express middleware to include in your app, and how to get the JSON out of the request body. What more do you need? – sgress454 Mar 05 '14 at 19:27

1 Answers1

0

The problem is in:

var info = {'nome':nome, 'email':email, 'assunto':assunto, 'mensagem':mensagem};

Just change the JSON ' ' to " " and works fine. I came from php and php recognize both ' ' and " " on a JSON, but nodejs just recognize strictly " ".

After some coding the result for a simple example of contact form is:

In a file common.js (front-end):

    var nome = $('#nome').val();
    var email = $('#email').val();
    var assunto = $('#assunto').val();
    var mensagem = $('#mensagem').val();

    var info = {"nome":nome, "email":email, "assunto":assunto, "mensagem":mensagem};

    $.ajax({
        type: "POST",
        url: "/contact",
        data: JSON.stringify(info),
        contentType:"application/json; charset=utf-8",
        dataType: 'json'
    }).done(function(retorno){
       //do something at the end
    }

In a file app.js or server.js or anything else (back-end):

var http = require("http")
    , express = require('express')
    , nodemailer = require('nodemailer')
    , app = express();

app.use(express.json());
app.use(express.static(__dirname+'/public'));

app.post('/contact', function(req, res){

    var name = req.body.nome;
    var email = req.body.email;
    var subject = req.body.assunto;
    var message = req.body.mensagem;

    var mailOpts, smtpTrans;

    smtpTrans = nodemailer.createTransport('SMTP', {
        service: 'Gmail',
        auth: {
            user: "your.user@gmail.com",
            pass: "your.password"
        }
    });

    mailOpts = {
        from: name + ' <' + email + '>', //grab form data from the request body object
        to: 'recipe.email@gmail.com',
        subject: subject,
        text: message
    };

    smtpTrans.sendMail(mailOpts, function (error, response) {
        //Email not sent
        if (error) {
            res.send(false);
        }
        //Yay!! Email sent
        else {
            res.send(true);
        }
    });

});

app.listen(80, '127.0.0.2');

From this example: http://blog.ragingflame.co.za/2012/6/28/simple-form-handling-with-express-and-nodemailer

Undefined Behavior
  • 2,128
  • 4
  • 24
  • 34