jquery ajax结果消息


jquery ajax results message

我有一个对php脚本的ajax调用,当用户提交数据字符串作为条目时,该脚本会用新记录更新我的MySQL数据库。PHP的一部分是if/else,它将检查条目是否重复。我可以阻止重复更新数据库,但我不知道如何发回一条"错误"消息,上面写着"该记录已经存在"。。。这是我的jquery:[脚本]$(function(({

    $(".entry_button").click(function() 
{
    var element = $(this);
    var boxval = $("#entry").val();
    var dataString = 'entry='+ boxval;
        if(boxval=='') {
            alert("Please Enter Some Text");
        } else {
            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle">&nbsp;<span class="loading">Broifying It...</span>');
            $.ajax({
                    type: "POST",
                    url: "update_data.php",
                    data: dataString,
                    cache: false,
                    success: function(html){
                        $("ol#update").prepend(html);
                        $("ol#update li").slideDown("slow");
                        document.getElementById('entry').value='';
                        $("#flash").hide();
                    }
            });
        }
        return false;
    });
});
[/script]

这是我的php脚本,用于更新数据库(update_data.php(:

[php]
include('db.php');
if(isset($_POST['entry']))
{
$date_created = date('Y-m-d H:i:s');
$entry = $_POST['entry'];
$query = "SELECT COUNT(*) AS count FROM table WHERE entry = '$entry'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
mysql_query("insert into table (entry,date_created) values     ('$entry','$date_created')");
$sql_in= mysql_query("SELECT entry,id FROM table order by id desc");
$r=mysql_fetch_array($sql_in);
$newentry=$r['newentry'];
$id=$r['id'];
?>
<li class="entry bar<?php echo $id; ?>">
<div class="thebro"><span><?php echo $newentry; ?></span></div>
<div class="hr"></div>
<div class="utility-left"><span class="share"><a href="#" title="Share">share</a></span> | <span class="time">days ago</span></div>
<div class="utility-right"><span class="comments">COMMENTS</span> | <span  class="like">LIKE</span></div>
</li>
<?php
} else {
$error = "Sorry, that record already exists";
}   
}
[/php]

我希望能够从php脚本中吐出$error变量,将其发送回ajax调用,并显示在HTML中的一个元素中。我在谷歌上搜索了ajax错误消息,但它们都与标头错误有关,而不是验证错误消息。我也不使用json_encode,除非有人想重做上面的jquery块,以便将其作为json数据发送。

任何想法都会很棒!

如果在PHP中产生错误,我希望通过jquery ajax对象的错误方法来处理,而不是通过发送一个带有一些错误标记responseText的普通HTTP 200响应来"伪造它"。

当从PHP返回HTTP500时,ajax错误处理程序不会捕获"responseText"。错误处理程序接收XHR对象、状态为"error"和错误消息"Internal Server error"。

我试图处理错误消息。。

header( 'HTTP/1.1 500 My Custom Error Message' );

但这并没有奏效。

起作用的是:

function errorHandler( $number, $message, $file, $line ) {
    $data = array(
        'message' => $message,
        'line' => $line,
        'file' => $file,
        'trace' => debug_backtrace()
    );
    header( 'Debug: ' . json_encode( $data, true ) );
}

这将使PHP返回HTTP500,并在一个额外的头中包含您的自定义详细信息。

要从ajax错误处理程序访问自定义详细信息:

$.ajax({
    success: function( response ) {
        // ...
    },
    error: function( XHR, status, errorThrown ) {
        $.each( XHR.getAllResponseHeaders().split( ''n' ), function( i, header ) {
            if ( header.substr( 0, 7 ) == 'Debug: ' ) {
                var error = JSON.parse( header.substr( 7 ) );
                console.log( error );
            }
        });
    }
});

只需在php文件中回显它并使用:

document.getElementById('ajaxCatchbox').innerHTML = ajaxRequest.responseText;

ajaxrequest对象的responsetext会拾取所有响应的内容。。。在jquery tho 中没有等价物

回显PHP中的错误并退出。然后,在你成功的开始:功能,做这样的事情:

    if(ajaxRequest.responseText.substring(0, 5) === 'Sorry') {
        alert(ajaxRequest.responseText);
        return;
    }

我建议使用子字符串方法,因为它允许您创建各种不同的错误消息,只要它们都以"对不起"开头。对调试很有用。