ngplayground
ngplayground

Reputation: 21617

$.ajax() response handling

I am trying to check my response of an ajax query but I can't seem to figure out how to parse the results.

$('input[name=sendpassword]').click(function(){
                var password = $("input[name=password]").val();
                var dataString = "uid=" + uid + "&password=" + password;
                console.log(dataString);
                $.ajax({
                    type:"POST",
                    url:"/assets/inc/check-password.php",
                    data:dataString,
                    dataType:'html',
                    context:document.body,
                    global:false,
                    async:false,
                    success:function(data){
                        console.log(data);
                        if (data[0].id==true){
                            console.log("success");
                        } else {
                            console.log("failed");
                        }
                        //window.location.replace(reffer);
                        //document.location ="/";
                    }
                });
            });

The console.log shows {"id":false} or {"id":true} but I'd like to do an if statement with the result

Upvotes: 0

Views: 101

Answers (4)

Ravi
Ravi

Reputation: 104

try this if (data.d==true)

$('input[name=sendpassword]').click(function(){
                var password = $("input[name=password]").val();
                var dataString = "uid=" + uid + "&password=" + password;
                console.log(dataString);
                $.ajax({
                    type:"POST",
                    url:"/assets/inc/check-password.php",
                    data:dataString,
                    dataType:'json',
                    context:document.body,
                    global:false,
                    async:false,
                    success:function(data){
                    console.log(data.d);
                        if (data.d==true){
                            console.log("success");
                        } else {
                            console.log("failed");
                        }
                        //window.location.replace(reffer);
                        //document.location ="/";
                    }
                });
            });

Upvotes: 2

Rab
Rab

Reputation: 35572

try this jQuery.parseJSON( data ) and instead of if (data[0].id==true){ use if (data.id=="true"){

$('input[name=sendpassword]').click(function(){
                var password = $("input[name=password]").val();
                var dataString = "uid=" + uid + "&password=" + password;
                console.log(dataString);
                $.ajax({
                    type:"POST",
                    url:"/assets/inc/check-password.php",
                    data:dataString,
                    dataType:'html',
                    context:document.body,
                    global:false,
                    async:false,
                    success:function(data){
                    data = jQuery.parseJSON(data)
                        console.log(data);
                        if (data.id==true){
                            console.log("success");
                        } else {
                            console.log("failed");
                        }
                        //window.location.replace(reffer);
                        //document.location ="/";
                    }
                });
            });

OR as suggested by Jared

dataType:'html' needs to be dataType:'json'.

then you wouldn't need to parse into JSON

Upvotes: 1

Sparky
Sparky

Reputation: 4879

Parse the result first, so you can access the individual components within the result.

var parsedResponse = JSON.parse(data)

Then access like parsedResponse.id, which you can use inside your if condition.

Upvotes: 0

Question Mark
Question Mark

Reputation: 3606

If the console just shows {"id":false} (i.e. not showing an array) then you can use it via: data.id==true

Upvotes: 0

Related Questions