AJAX函数永久性错误

时间:2011-06-10 16:47:00

标签: php jquery ajax

这个AJAX功能出了问题,但我看不出来。

AJAX

$('.review').click(function() {
    var idatas = $(this).attr('rel');
    var idata = 'idata=' + idatas;

    $.ajax({
        type: 'POST',
        url: '<?php echo $thisposturl;?>?review',
        data: idata,
        beforeSend: function() {
            $(this).addClass('ractive');
        },
        dataType:'json',
        success: function(data) {
            $('#dbody').html(data.ioutput);

            $('.cright').height($('#dropout').height());
            $('#dropout').addClass('dropopen');
        },
        error: function(data) {
             $('#dbody').html('arse biscuity');
             $('.cright').height($('#dropout').height());
             $('#dropout').addClass('dropopen');
        }
    });

PHP

<?php 

$rid = $_POST['idata'];

$ireviews = get_posts('post_type=reviews&p='.$rid.'&numberposts=-1');

foreach ($ireviews as $ireview) :   
setup_postdata($post);

$ioutput = '<div class="iavatar"></div>
<div class="ititle">'.get_the_title($ireview).'<br />
<div class="iauthor">'.get_the_author($ireview).'</div></div>
<div class="icontent">'.get_the_content($ireview).'</div>';

endforeach;
echo json_encode(array('ioutput'=> $ioutput));


?>

据Firebug说,答案是这个

{"ioutput":"<div class=\"iavatar\"><\/div>\n<div class=\"ititle\">What a lovely course<br \/>\n<div class=\"iauthor\">pm master<\/div><\/div>\n<div class=\"icontent\">This intimate layout, with its undulating and springy fairways that zigzag in amongst the woodland setting, calls for accurate driving and precision shot-making into the well-bunkered greens; another classic Colt trademark. Position, not power, is the name of the game here.<\/div>"}

但它会出现错误功能而不是将内容放在#dbody

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用$.ajaxSetup()来获取正确的错误:

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});

如果Ajax调用出现任何错误,您将获得适当的警报。