【发布时间】:2012-03-14 11:02:20
【问题描述】:
以下代码警告“未定义”,而不是按照我的预期从响应数据中附加 html。有谁知道为什么?
JavaScript:
$(function() {
$('.document').on('click', '.ajax', function(e) {
e.preventDefault();
// ajax request
$.ajax({
async: true,
cache: false,
type: 'post',
url: '/echo/html/',
data: {
html: '<p>This is echoed the response in HTML format</p>',
delay: 1
},
dataType: 'html',
beforeSend: function() {
console.log('Fired prior to the request');
},
success: function(data) {
console.log('Fired when the request is successfull');
$('.document').append(data);
},
complete: function() {
console.log('Fired when the request is complete');
}
});
});
});
HTML:
<div class="document">
<a class="ajax" href="#">Fire an AJAX request</a>
</div>
示例 jsFiddle:http://jsfiddle.net/L6bJ2/3/
【问题讨论】:
-
您的代码中没有警报,但我认为您的“警报”执行速度比您的 ajax 请求快,所以当您尝试提醒 ajax 结果时,没有,因为 ajax 没有到此结束。