title: Node JS Form Sumit using Ajax
body: <p>I am new in Node JS , I want to submit a form using ajax like all we are doing in PHP/CakePHP but here i am facing a problem.</p>
  
  <p><strong>HTMl code</strong></p>
  
  <pre><code>&lt;form role="form" action="javascript:;" id="myform"&gt;
          &lt;div class="form-group"&gt;
            &lt;label for="name"&gt;Name:&lt;/label&gt;
            &lt;input type="text" class="form-control" id="name" name="name" placeholder="Enter Name"&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
            &lt;label for="email"&gt;Email:&lt;/label&gt;
            &lt;input type="email" class="form-control" id="email" name="email" placeholder="Enter email"&gt;
          &lt;/div&gt;
          &lt;button type="submit" class="btn btn-default" id="enter"&gt;Submit&lt;/button&gt;
        &lt;/form&gt;
  </code></pre>
  
  <p><strong>My AJAX code is</strong> </p>
  
  <pre><code>$('.btn').click(function(e){ 
            e.preventDefault();
            var data = $('#myform').serialize();
            $.ajax({ 
                url: '/ajax',
                type: 'POST',
                cache: false, 
                data: JSON.stringify(data),
                contentType: 'application/json',
                success: function(data) {
                            console.log(data);
                            console.log(JSON.stringify(data));
                          },
                error: function(jqXHR, textStatus, err){
                     alert('text status '+textStatus+', err '+err);
                 }
              })
            });            
  </code></pre>
  
  <p><strong>app.js code</strong></p>
  
  <pre><code>app.post('/ajax', bodyParser(), function (req, res){
  
    var obj = {};
    console.log('body: ' + JSON.stringify(req.body));
  
    var input = JSON.stringify(req.body);
    var data = {
  
        name    : input.name,
        email   : input.email
    };
  
    var query = db.query("INSERT INTO users set ?",data, function(err, rows){
    console.log(query.sql);
        if (err)
            console.log("Error inserting : %s ",err );
           res.send({'success' : true, 'message' : 'Added Successfully'});
    });
  
  
  });
  </code></pre>
  
  <p>But when i submit this form then it generate an error in node console like</p>
  
  <pre><code>SyntaxError: Unexpected token "
     at parse (D:\man_node\node_modules\body-parser\lib\types\json.js:83:15)
  </code></pre>