D555 D555 - 3 years ago 145
Javascript Question

Jquery ajax error callback is executed even though the response http status is 200

I have written simple ajax code, in success callback I have written an alert but it does not work.

The code is:

$(".change_forex_transaction_status").click(function(){
$("#insufficient_funds").css("display","none");
var id = $(this).attr('data-transaction_id');
//var ttype = $(this).attr('data-ttype');
if (confirm("Are you sure you want to mark this transaction as complete?")) {
$(this).unbind("click");
$.ajax({
url:"<?php echo Yii::app()->getBaseUrl(true);?>/customer/changeForexTransactionStatus",
type:"POST",
dataType: "json",
//data:{id:id,tidentify:2,ttype:ttype},
data:{id:id,tidentify:2},
success:function(res){
if(res == "unauthorized"){
alert("You Are not authorize to perform this action.");

}else{
if(res == "success"){
location.reload();
} else if(res == "insufficient_fund"){
alert('Insufficient Fees');
$("#insufficient_funds").css("display","block");
} else if(res == 'invalid_fee_account'){
alert('Invalid Merchant Fees Account');
}
}
},
error:function(t) {
console.log(t);
}
});
}
});


Even though the response http status code is 200, it goes into error callback whereas it should have gone in success callback and opened an alert box.

Can anyone please help on this.

Answer Source

You are expecting json back not text so change the ajax dataType to text

 dataType: "text",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download