【问题标题】:Ajax Alert Response from PHP来自 PHP 的 Ajax 警报响应
【发布时间】:2017-03-23 18:20:42
【问题描述】:

希望这里是一个简单的问题。我实际上使用了我在 SO 上找到的一个示例,但无法弄清楚它为什么不起作用。控制台或任何东西都没有错误。

我有一个 ajax Post 函数,用于将数据传递给 php 脚本。

它正确地传递了数据,但每次响应都会作为错误警报返回。我可以确认服务器端正在获取数据并正确处理它,只是无法弄清楚为什么它从不返回成功响应。

这里是 Ajax:

    $(function () {
        $('#pseudoForm').on('click', '#submit', function (e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "psu_output.php",
                data: $('#pseudoForm').serialize(),
                datatype: 'json',
                success: function (response) {
                    if(response.type == 'success') {
                        $('#messages').addClass('alert alert-success').text(response.message);
                    } else {
                        $('#messages').addClass('alert alert-danger').text(response.message);
                    }
                }
            });
            return false;
        });
    });
</script>

在我的 php 脚本中我使用了这个:

<?php

$success = true;

if($success == true) {
    $output = json_encode(array('type'=>'success', 'message' => 'YAY'));
} else {
    $output = json_encode(array('type'=>'error', 'message' => 'WHOOPS'));
}

die($output);
?>

【问题讨论】:

  • 在运行时,response 实际包含什么?
  • 使用console.log(response)查看响应。
  • 尝试回显 $output,而不是 die()
  • @guyfawkes die() 回显它的参数,如果它是一个字符串。

标签: javascript php jquery ajax response


【解决方案1】:

问题是datatype: 'json' 应该是dataType: 'json'。 Javascript 区分大小写。

【讨论】:

  • 感谢您的回复。对于其他人阅读,我实际上必须做两件事:1)修复错字,2)把它放在我的 php 脚本的其余部分之上。我在下面有它,但它没有正确返回。 (嗯,它正在发送响应,但 ajax 没有看到它)。
【解决方案2】:

错误是因为您收到的返回数据为json,但内容类型是简单的字符串(文本/html),所以您需要先JSON.parse()收到的数据,如下所示:

$(function () {
    $('#pseudoForm').on('click', '#submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "psu_output.php",
            data: $('#pseudoForm').serialize(),
            datatype: 'json',
            success: function (response) {
            response = JSON.parse(response);
            if(response.type == 'success') {
                    $('#messages').addClass('alert alert-success').text(response.message);
            } else {
                    $('#messages').addClass('alert alert-danger').text(response.message);
                }
            }
        });
        return false;
    });
});

第二种选择是从 php 本身发送 json 标头,从而无需在 javascript 中解析 JSON。您可以使用以下代码行在从 PHP 脚本回显或打印任何其他内容之前

header('Content-Type: application/json');

然后

echo $output;

【讨论】:

  • dataType: 'json' 使其自动解析。问题是他有一个错字。
【解决方案3】:

如果您使用 JSON 响应,则需要设置标头,以便您的浏览器和 JavaScript 可以正确解释它:

<?php

$success = true;

if ($success == true) {
    $output = json_encode(array(
        'type' => 'success',
        'message' => 'YAY'
    ));
} else {
    $output = json_encode(array(
        'type' => 'error',
        'message' => 'WHOOPS'
    ));
}

header('Content-Type: application/json');
echo $output;

【讨论】:

  • 如果你使用dataType: 'json',你就不需要这个。问题是他有一个错字。
  • 有很多方法可以解决,你的就是其中之一,我的也是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2022-08-06
  • 1970-01-01
相关资源
最近更新 更多