psuresh
psuresh

Reputation: 584

Retrieve response text in AJAX

I have sent an array to AJAX to return the status if the query is executed.

if(mysqli_query($dbconfig,"INSERT INTO todo(description) values('$desc')")){
        $response['success']="true";
}
header('Content-type: application/json');
echo json_encode($response);

In client side script, I tried following code to show message according to the status.

if(response.status=="success"){
      alertify.success("New item has been added successfully");
}else if(response.status=="error"){
      alertify.error("Error while adding the item");
}

Even the query runs correctly it doesn't satisfy both the cases. In my console log, status shows success.

enter image description here

Upvotes: 0

Views: 55

Answers (1)

Pablo Recalde
Pablo Recalde

Reputation: 3571

Based on the code you provided you can do two things

if(response.status === 200){
    //handle success
} else {
    //handle error
}

Or if you like, you could try with:

var status = JSON.parse(response.responseText);
if(status.status === "success"){
    //handle success
}else{
    //handle error
}

Upvotes: 1

Related Questions