title: Express POST - Returned JSON is treated as invalid by JQuery
body: <p>I am trying to write a small authentication service using Express and Node.</p>
  
  <p>I did a search on SO and don't seem to find my answer
  even though there're many similar questions but no
  definitive answer really.</p>
  
  <p>I tried many variations of my server side code but
  seems I am still missing something.</p>
  
  <p>The POST call is made from an HTML page 
  with some JQuery code (ajax call).</p>
  
  <p>I enter the post() method in Express but when
  it returns response to the HTML page, always the ajax
  error handler is executed, never the success handler.</p>
  
  <p>My JSON which I return seems valid to me.
  I tried calling send and json on the 
  response object but nothing really works.</p>
  
  <p>What am I missing?</p>
  
  <p>Any help would be greatly appreciated.
  Thanks in advance.</p>
  
  <pre><code>var mod = require('express');
  
  var auth = require('./login_module.js'); // my module
  
  var express = require('express');
  var app = express();
  
  app.use(express.bodyParser());
  
  app.post('/login', function(request, response) {
  
     console.log("post method called");
  
     var credentials = request.body;
     console.log("credentials = " + credentials);
     console.log(credentials);
  
     auth.authenticate(credentials.username, credentials.password, function(result){
          console.log("Authentication Result: " + result);
          var code = result === 1 ? 200 : 401;
          console.log("Response Code: " + code);
          var res = {
              data : "Response Code: " + code
          };
          console.log(JSON.stringify(res));
  
          // So far I am good!
  
          response.statusCode = code;
          response.json(res);
  
          // Response is now sent
          // but not recognized as 
          // valid JSON in the client.
          console.log("response sent");
     });
  
  });
  
  app.listen(10101);
  </code></pre>
  
  <p>JQuery call.</p>
  
  <pre><code>  &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
              $( "#btn" ).click(function(){
                    alert('calling now!');
                    var obj = {
                      username: $('#usrn').val(),
                      password: $('#pwd').val()
                    };
                  $.ajax({
                      type: "POST",
                      url: 'http://localhost:10101/login',
                      data: obj,
                      success: function (data, textStatus, jqXHR){
                          alert('got response back!');
                          if ("200" === textStatus){
                              $('#status').text('Login succeeded!');
                          }else if ("401" === textStatus){
                              $('#status').text('Login failed!');
                          }else{
                              $('#status').text('Invalid status received: ' + textStatus);
                          }
                      },
                      error : function(jqXHR, textStatus, errorThrown){
                          alert("Error when getting response.");
                      },
  
                      dataType: 'json'
                  });
              })
          });
      &lt;/script&gt;
  </code></pre>