【问题标题】:Even after success, ajax success call executing else condition即使成功后,ajax成功调用执行else条件
【发布时间】:2021-02-20 15:27:26
【问题描述】:

我根据 if 和 else 条件从 php 返回响应。即使在执行 if 条件之后,ajax 成功调用也会执行 else 条件。下面是我的代码。我没有让交易成功,而是没有足够的余额。我尝试了很多方法,但没有奏效。任何帮助都会很棒。

php代码

<?php
    if(isset($_POST['name'])){
        // some code
    }

    if(isset($_GET['paymentInfo']))
    {
        // some code
        if($dataPayerBalance >= $amount)
        {
            // some code
            $response = 'success'; 
        }
        else
        {
            $response = 'error'; 
        }
        // some code
        echo json_encode($response);
    }
?>

ajax 调用

 $.ajax({
            type: 'get',
            data: {'paymentInfo' : dataToSend},
            success: function(response)
            {
                if(response == 'success')
                {
                    $('.modal-body').html("Transaction Successful");
                    $('#empModal').modal('show');
                }
                else
                {
                    $('.modal-body').html("Don't have enough balance");
                    $('#empModal').modal('show');
                }
                            
            },
            error: function()
            {
                $('.modal-body').html("Error Occured");
                $('#empModal').modal('show');
            }
    });

【问题讨论】:

    标签: php json ajax


    【解决方案1】:

    问题是您将字符串编码为 json 并且期望响应中的不是 json,而是字符串。让我们把它们都变成 jsons:

    在您的 php 代码中:

    echo json_encode(['result'=>$response]);
    

    在您的 ajax 调用中:

    $.ajax({
        url: "your api url",
        type: 'GET',
        data: {'paymentInfo' : dataToSend},
        dataType: 'json'
    }).done(function( data ) {
        if (data.result == 'success'){
            //your custom action
        }
    })
    .fail(function() {
        //your custom action
    });
    

    【讨论】:

    • 它仍然提醒没有足够的余额
    • 作为响应在网络选项卡下,我得到 json 和所有其他 html 代码。这是问题吗? {"result":"success"} // 更多代码........这是问题吗?
    • 我已经编辑了 js 部分。试试看。
    【解决方案2】:

    试试这个!

    PHP

     <?php
    
        if(isset($_POST['name'])){
            // some code
        }
    
        if(isset($_GET['paymentInfo']))
        {
            $response = array();
            // some code
            if($dataPayerBalance >= $amount) {
                // some code
               $response = ('status'=>'success');
            }
            else {
                   $response = ('status'=>'error');
            }
            // some code
            echo json_encode($response);
        }
      ?> 
    

    AJAX:

       $.ajax({
                type: 'get',
                data: {'paymentInfo' : dataToSend},
                dataType: 'json',
                success: function(response)
                {
                   var jsonString = JSON.parse(response);
    
                   if(jsonString.status == 'success')
                    {
                        $('.modal-body').html("Transaction Successful");
                        $('#empModal').modal('show');
                    }
                    else
                    {
                        $('.modal-body').html("Don't have enough balance");
                        $('#empModal').modal('show');
                    }
                                
                },
                error: function()
                {
                    $('.modal-body').html("Error Occured");
                    $('#empModal').modal('show');
                }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多