简体   繁体   English

即使响应http状态为200,也会执行jQuery ajax错误回调

[英]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. 我已经编写了简单的ajax代码,在成功的回调中我已经编写了一个警报,但是它不起作用。

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. 即使响应http状态代码为200,它也会进入错误回调,而应该返回成功回调并打开警报框。

Can anyone please help on this. 任何人都可以帮忙。

您期望json返回不是文本,所以将ajax dataType更改为text

 dataType: "text",

Use JSON.stringify to post data on server, and when you post data to server in json so use content type "application/json" . 使用JSON.stringify将数据发布到服务器上,并且当您使用json将数据发布到服务器时,请使用内容类型"application/json" Now if you expect data in json from server then use dataType: "json" . 现在,如果您希望从服务器获取json中的数据,请使用dataType: "json" If data from server is html then you can use dataType: "html" or it is text then you can use dataType: "text" . 如果来自服务器的数据为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM