[英]jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK
[英]Jquery ajax error callback is executed even though the response http status is 200
我已經編寫了簡單的ajax代碼,在成功的回調中我已經編寫了一個警報,但是它不起作用。
代碼是:
$(".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);
}
});
}
});
即使響應http狀態代碼為200,它也會進入錯誤回調,而應該返回成功回調並打開警報框。
任何人都可以幫忙。
您期望json返回不是文本,所以將ajax dataType更改為text
dataType: "text",
使用JSON.stringify
將數據發布到服務器上,並且當您使用json將數據發布到服務器時,請使用內容類型"application/json"
。 現在,如果您希望從服務器獲取json中的數據,請使用dataType: "json"
。 如果來自服務器的數據為html,則可以使用dataType: "html"
或為text,則可以使用dataType: "text"
。
data: JSON.stringify({ id: id, tidentify: 2 }),
contentType: "application/json",
dataType: "json"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.