0

当您使用 Jquery 或 Ajax 调用加载到 div 中的页面或脚本时,或示例

$.post('pagename.php', $(#cpay'.$id.'").serialize(), function(data) { $('#conSupp".$id."').html(data);

如果页面加载失败,有没有办法在 div 中显示错误消息

非常感谢

4

3 回答 3

3

如果您使用的是 jquery 1.5 或更高版本,该帖子将返回一个 jqXhr 对象。如果是这样,您可以执行以下操作:

$.post('pagename.php', data, function(data) {
    //success, do stuff with the data object
}).fail(function(jqXhr, ajaxOptions, thrownError){
    //something went wrong
    alert('Error: ' + thrownError);
});
于 2013-02-18T15:20:32.300 回答
1

是的,通过调用调用返回.fail()的对象上的函数来绑定错误处理程序:jqXhr$.post()

$.post(url, data, function(data) {
    ...
}).fail(function(jqXhr, textStatus, errorThrown) {
    // an error occurred - do something here
});
于 2013-02-18T15:20:41.667 回答
1

我更喜欢这样做:

$.ajax({
    type: 'POST',
    url : 'pagename.php',
    data: $(#cpay'.$id.').serialize()
}).done(function(data {
    $('#conSupp".$id."').html(data);
}).fail(function() {
    $('#conSupp".$id."').html('An error occured');
});

它有点长,但我发现阅读对我需要的任何内容的更改要容易得多,而且这正是 $.post 在内部所做的,所以为我节省了一个函数调用?

于 2013-02-18T15:22:08.787 回答