php - 基于 PHP 通过 jQuery 响应的条件语句

标签 php javascript jquery response

我的表单通过 jQuery 从 PHP 返回数据。如何根据 jQuery 中的响应创建条件语句。

这是 jQuery:

  $.ajax({
                    type: "POST",
                    url: "createAlbum.php",
                    data: postData, 
                    success: function(data){
                        $('#message').fadeIn();
                        $('#message').html(data);
                    }
                  });

这是从 PHP 返回的内容:

if($error) {
        echo $error;
    }
    else {
        echo $success;
    }

因此,如果响应成功,该消息应在几秒钟后隐藏,但如果是错误,则应等到用户更正它。像这样的事情:

success: function(data){
                    $('#message').fadeIn();
                    $('#message').html(data);

                    if (data.response == 'success') {
                        alert('Success');
                        setTimeout(function() {
                            $('#message').fadeOut();
                            }, 5000 );
                    } else if (data.response == 'error') {
                            alert('Error');
                       }
            }

最佳答案

为什么不使用 statusCode 设置来调用 ajax()并拥有比“成功”和“错误”更细粒度的控制?您的脚本可能(或将会)有多种不同的方式成功或失败。

statusCode(added 1.5)Map Default: {}

A map of numeric HTTP codes and functions to be called when the response has the corresponding code. For example, the following will alert when the response status is a 404:

$.ajax({ statusCode: { 404: function() { alert('page not found'); } } });If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error, they take the same parameters as the error callback.


更新:

我添加了一个示例,说明如何在 Jquery 中使用 statusCode 而不是 ajax() 调用的成功和错误设置。一、测试UI:

<html>
<head>
<title>jQuery Test Page</title>
<script type="text/javascript" src="jquery-1.6.js"></script>
<script type="text/javascript">
function displayMessage(message){
    $('#message').fadeIn();
    $('#message').html(message);
}

function successFunction(data){
    displayMessage(data);
}

function notFoundFunction(){
    displayMessage("Not Found!");
}

function errorFunction(){
    displayMessage("An Error Occurred!");
}

function invokeAjax(test){
    $.ajax({
        type: "POST",
        url: "test.php?test="+test,
        //data: postData,
        statusCode: {
            200: successFunction,
            404: notFoundFunction,
            500: errorFunction,
        }
    });
}
</script>
</head>

<body>
<h1 id="message" style="display:none;"></h1>
<a href="javascript:invokeAjax('success');">Invoke Ajax - Success (200)</a><br>
<a href="javascript:invokeAjax('notfound');">Invoke Ajax - Not Found (404)</a><br>
<a href="javascript:invokeAjax('error');">Invoke Ajax - Error (500)</a><br>
</body>
</html>

这是 Ajax 调用的 PHP 脚本:

<?php

$test = $_GET['test'];

if($test == 'success'){
    echo 'some meaningful data';
} else if($test == 'notfound') {
    http_send_status(404);
} else if($test == 'error'){
    http_send_status(500);
}
?>

我认为这是不言自明的,但如果您还有任何其他问题,请随时询问。

关于php - 基于 PHP 通过 jQuery 响应的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889582/

相关文章:

javascript - 从 Bootstrap slider 获取变量

javascript - 将参数传递给 animate jquery 函数

javascript - 在表格上调整窗口大小

javascript - 使用 jquery 拖放时如何将排序的表行存储在 cookie 中?

php - mysql_result() 期望参数 1 是资源,给定的 bool 值

php - Ubuntu Apache PHP - 创建和写入 Excel 文档

javascript - 如何在 Loopback 中实现 ACID 事务

jquery - 哪些 jQuery 模式弹出窗口或灯箱插件可以在 IE7、IE8、IE9 Firefox、Chrome、Safari 中工作?

php - 我认为 PHP 将我的输入视为八进制,让它停止吗?

php - 如何在数据库的php sdk4中存储facebook的GraphObject?