以下代码警告“未定义”,并且不会像我预期的那样在响应数据中附加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/
答案 0 :(得分:9)
HTTP方法由type
而不是method
指定,因此您应该使用;
type: 'post',
因为您已将响应类型指定为HTML,所以您将获得data
回调的success
参数中传递的字符串;但是当你尝试使用data.html
时,你似乎期待JSON。相反,直接使用data
;
success: function(data) {
console.log('Fired when the request is successfull');
$('.document').append(data);
},
通过这些更改,您会找到it works: http://jsfiddle.net/L6bJ2/6/
答案 1 :(得分:0)