我无法从我的ajax请求中获得出现在<div class="l_p_i_c_w"></div>
中的data
。我做错了什么?我知道my_file.php
内部的函数是有效的,因为如果我刷新页面,那么数据就会显示在它应该显示的地方。
jQuery:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
}
});
HTML:
<div class="l_p_w" id="myID">
<div class="l_p_c">
<div class="l_p_i_c_w">
<!-- stuff, or may be empty. This is where I want my ajax data placed. -->
</div>
</div>
</div>
CSS:
.l_p_w {
width:740px;
min-height:250px;
margin:0 auto;
position:relative;
margin-bottom:10px;
}
.l_p_c {
position:absolute;
bottom:10px;
right:10px;
width:370px;
top:60px;
}
.l_p_i_c_w {
position:absolute;
left:5px;
top:5px;
bottom:5px;
right:5px;
overflow-x:hidden;
overflow-y:auto;
}
发布于 2012-11-22 04:43:04
我认为如果你使用prepend,你需要在追加之前将数据对象包装在像$( data )这样的jquery标记中,因为prepend会附加子对象(对象)。
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').prepend($(data));
}
});
但是,如果您只想使用数据设置div的html,请执行以下操作:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').html(data);
}
});
第三个选项,尝试prependTo
http://api.jquery.com/prependTo/
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$(data).prependTo($('div#myID div.l_p_c div.l_p_i_c_w'));
}
});
最后一次尝试:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
success: function(data){
$('div#myID div.l_p_c div.l_p_i_c_w').html(data + $('div#myID div.l_p_c div.l_p_i_c_w').html());
}
});
发布于 2012-11-22 04:59:50
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
应该是
$('#myID .l_p_c .l_p_i_c_w').html(data);
发布于 2012-11-22 04:28:12
试试这个:
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html',
complete: function(jqXHR, settings){
if (jqXHR.status == 200)
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(jqXHR.responseText);
}
});
或
$.ajax({
type: "POST",
url: "my_file.php",
dataType: 'html'
}).done(function(data) {
$('div#myID div.l_p_c div.l_p_i_c_w').prepend(data);
});
https://stackoverflow.com/questions/13505854
复制相似问题