成功响应中的json变量无效

时间:2019-02-12 11:53:46

标签: jquery ajax cakephp

我正在尝试使用ajax发送帖子数据。但是当我收到json响应时。错误是无效的json变量。
这是我的ajax代码。

if(flagSubmit == true){
        var postData = $('#frm-dth-recharge').serialize();
        //console.log(postData);
        $('#rechargedth').hide();
        $('#loadingdthgif').css('display','block');
        $.ajax({
            type: "post",
            url: "<?php echo $this->webroot;?>recharges/processdtherecharge",
            data: postData,
            contentType: "application/x-www-form-urlencoded",
            success: function(recharge_status) {
                //recharge_status = recharge_status.trim();
                var recharge_status = $.parseJSON(recharge_status);
                var  res1 = recharge_status.rechargeStatus;
                var res2 = recharge_status.walletamount;
                if (res1 == "SUCCESS") {
                    msg = "Congratulation, Your recharge has been Done.";
                } else if (res1 == "PENDING") {
                     msg = "Pending, Some technical issue your recharge is in processing.";
                } else if (res1 == "FAILED") {
                     msg = "Sorry, for inconvenient your recharge was not Done.";
                } else {
                     msg = "Sorry, for inconvenient your recharge was not Done.";
                }
                $('#rechargedth').show();
                $('#loadingdthgif').css('display','none');

                $('#frm-dth-recharge').trigger("reset");
                alert(msg);
                $("#dthwallamount").text(res2);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    }else{
        return flagSubmit;
    }
}

这是我的回复Json Variable。我不会在哪里我错了。 请帮忙。

{"STATUS":"PENDING","MOBILE":"30457807","AMOUNT":"50","RPID":"XXXXXX52","AGENTID":"96ea4787d3","OPID

":"","BAL":1149.95,"MSG":"PENDING"}{"rechargeStatus":"SUCCESS","walletamount":"40.5895"}

1 个答案:

答案 0 :(得分:0)

您需要从ajax方法中删除波纹管

contentType: "application/x-www-form-urlencoded",

您会没事的。