0

I am submitting a form using jquery ajax, I can send the request but cannot recieve a json reponse in success callback. Even though I have set the dataType or content-type to json in ajax and it says Invalid json. Below is the code.

$("#loginForm").submit(function() {
    var url = 'localhost:8080/login';

    $.ajax({
        type: "POST",
        url: url,
        data: $("#loginForm").serialize(), // serializes the form's elements.
        success: function(data)
        {
            alert(JSON.parse(data));
        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert(textStatus);
        }
    });
    return false;
});

// serverside node.js

res.set('Content-Type', 'application/json');
res.setHeader("Access-Control-Allow-Origin", "*");

var userName = req.param('userName');
var password = req.param('password');

res.status(404);
res.json({result : "Invalid User"});

how will I recieve this result in success callback.

JN_newbie
  • 5,492
  • 14
  • 59
  • 97
  • 1
    With status code 404 you will not enter success callback by definition. – dfsq Feb 28 '15 at 09:52
  • 1
    `data` parameter of `success` handler is already parsed Javascript object (if your request works properly). So there is no need to call `JSON.parse` (which expects a `String`) – hindmost Feb 28 '15 at 09:52
  • @dfsq Thakyou very much that was the issue – JN_newbie Feb 28 '15 at 09:54

0 Answers0