Something is wrong with this AJAX function but I do not see what.
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['开发者_JAVA百科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));
?>
According to Firebug the response is this
{"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>"}
But it is going to error function and not putting the content in #dbody
Any ideas?
Try using the $.ajaxSetup() to get the correct error like this:
$(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);
}
}
});
});
In case of any error in the Ajax call, you will get proper alert.
精彩评论