Aanshi
Aanshi

Reputation: 93

when passing JSON request ajax call failed

I am calling fallowing function

function checkUser(){
    $.ajax({
        type: "POST", 
        url: "assets/json/login.json",
        dataType: 'json',
        data: {name:"XYZ",status:'A'},          
        success: function(data)
        {
            obj = JSON.parse(data);
            alert(obj.status);
            if(obj.status == 200)
            {
                window.location='dashboard.html'
            }else
            {
                    $("#error").html(obj.msg);
                    return false;
            }

        },

     error: function(errorThrown){
            alert("There is an error with AJAX!" + errorThrown);
    } 
});

}

In response, the error function is called. Json file contain status and message like {status:"200",msg:"SUCCESS"}

EDIT I corrected my json. Now it look like this {"status":"200","msg":"SUCCESS"} Now its calling Success function. But Its giving following error on obj = JSON.parse(data);

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

Here response is already in json format so I think no need to parse it. But then how to display status ?

Upvotes: 1

Views: 1173

Answers (2)

Jeremy Thille
Jeremy Thille

Reputation: 26370

You're expecting a JSON in the dataType. You're receiving a JSON. So no need to parse it. Remove JSON.parse(data).

Upvotes: -3

Sujith
Sujith

Reputation: 206

Will making response keys as string helps? Instead of

{status:"200",msg:"SUCCESS"}

can you try

{"status":"200","msg":"SUCCESS"}

Upvotes: 3

Related Questions