【问题标题】:Alert only "message" portion of {"success":"false","message":"Credit card number is invalid. "}仅提醒 {"success":"false","message":"信用卡号无效。"} 的“消息”部分
【发布时间】:2015-06-30 00:51:41
【问题描述】:

从标题中可以看出,我希望弹出警报显示“信用卡号无效。”

我目前正在使用 alert(data.responseText);但显示“信用卡号在与订阅相关联时无法更新为不支持的卡类型。
此商家帐户不接受信用卡类型。
信用卡号无效。
{” success":"false","message":"信用卡号无效。"}"

有没有办法只显示“信用卡号无效”部分。

如果我使用 alert(data.Message));给出“未定义”和警报(数据);给出“[object Object]”。

感谢您的帮助。

var data = {
                "action": "checkout"
            };
            data = $('#checkout_form').serialize() + "&" + $.param(data);
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "inc/placeorder.php", //Relative or absolute path to response.php file
                data: data,
                success: function(data) {
                    if(data["success"] == 'true'){
                        $("#placeorder").attr("disabled",false);
                        $(".payment_loader").fadeOut("slow").hide();
            $('#checkout_form').trigger("reset");
                        window.location = "order_success.php"; 

                    }
                    if(data["success"] == 'false'){
                        $("#placeorder").attr("disabled",true);
                        $(".payment_loader").fadeOut("slow").hide();
                       $('#subscribe_response').html(data['message']);
                      alert(data.responseText);

                    }
                },
                error: function(data, status){
                    alert(data);

                    console.log("error", data.responseText);
                    console.log("error", status.responseText);

}
            });                         
        }else{
            return false;
        }
    });

编辑

这是被抛出的错误。

} else {

    foreach ($result->errors->deepAll() as $error) {

        $errorFound = $error->message . "<br />";

        echo $errorFound;


    }

    $response = array("success" => "false", "message" => $errorFound);

    return $response;

    exit;

}

【问题讨论】:

  • alert(data.message); ?
  • 提醒前行中的data['message']怎么样?
  • 如果你的意思是 alert(data['message'];

标签: php ajax json


【解决方案1】:

实际上你在你的ajax请求中设置了响应类型“json”,但是你没有在“json”中得到响应。在你的php脚本中使用json_encode函数来返回json响应

return json_encode(response);

然后提醒

alert(data.message);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-22
    • 2010-12-01
    • 2017-05-30
    • 2013-07-23
    • 2023-02-17
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多