2017-07-25 102 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" 
相关问题