Функция AJAX при постоянной ошибке - PullRequest
0 голосов
/ 10 июня 2011

Что-то не так с этой функцией 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 голосов
/ 28 января 2013

Попробуйте использовать $. 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 вы получите соответствующее предупреждение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...