2017-07-25 73 views
0

我已經編寫了簡單的ajax代碼,在成功回調中我寫了一個警報,但它不起作用。即使響應http狀態爲200,也會執行jquery 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,它進入錯誤回調,而它應該去的成功回調,並打開一個警告框。

任何人都可以請幫忙。

+1

所以....它顯示什麼錯誤?還有什麼顯示在控制檯中,還有其他錯誤或消息? –

回答

3

你期待JSON回來不是文本所以更改AJAX數據類型爲文本

dataType: "text", 
+0

另外,你可以補充說,如果OP想發送'JSON',他必須添加一個'contentType:「application/json」'。 –

+0

@ ADreNaLiNe-DJ取決於服務器端的實現 – madalinivascu

+0

你是對的,但人們經常誤解'contentType'和'dataType'。 –

1

使用JSON.stringify到服務器上的數據後,當你在JSON數據發佈到服務器,以便使用內容類型"application/json"。現在,如果您希望從服務器獲取json中的數據,請使用dataType: "json"。如果服務器的數據是html,那麼你可以使用dataType: "html"或者它是文本,那麼你可以使用dataType: "text"

data: JSON.stringify({ id: id, tidentify: 2 }), 
contentType: "application/json", 
dataType: "json" 
相關問題