Javascript 如何在不刷新的情况下将新内容加载到DIV
我想在不刷新整个页面的情况下将新内容从服务器加载到DIV。 我尝试了以下方法Javascript 如何在不刷新的情况下将新内容加载到DIV,javascript,jquery,html,ajax,web-deployment,Javascript,Jquery,Html,Ajax,Web Deployment,我想在不刷新整个页面的情况下将新内容从服务器加载到DIV。 我尝试了以下方法 $.ajax({ type: "GET", url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData +"#", success: function (response) { $("#te
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+
occasionData +"&relationship="+ forData +"#",
success: function (response) {
$("#testDIV").html(response);
}
});
但问题是整个页面都在
中加载。我想用从服务器返回的新DIV内容替换旧DIV,而不是整个页面。您可以保持您对直接使用AJAX感兴趣的流程感,并希望管理您的已完成功能(而不是因为成功)。这里是如何
.done(function(data) {
...
}
在done函数中,您可以根据需要过滤页面内容。只需像这样用jquery请求要过滤的内容
var $response = $(data);
var response_title = $response.filter(".title").html(); //I'm assuming you are trying to pull just the title (perhaps an < h1 > tag, in this little example, from the entire external page!
$.ajax({
type: "GET",
url: "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData +"#"})
.done(function(response) {
var $response = $(response);
var response_title = $response.filter(".title").html();
$("#testDIV").html(response_title);
});
使用done函数,基于,可以使用此格式
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
所以你的最终代码可能看起来像这样
var $response = $(data);
var response_title = $response.filter(".title").html(); //I'm assuming you are trying to pull just the title (perhaps an < h1 > tag, in this little example, from the entire external page!
$.ajax({
type: "GET",
url: "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData +"#"})
.done(function(response) {
var $response = $(response);
var response_title = $response.filter(".title").html();
$("#testDIV").html(response_title);
});
您可以保持对直接使用AJAX感兴趣并希望管理已完成功能(而不是因为成功)的相同流程感觉。这里是如何
.done(function(data) {
...
}
在done函数中,您可以根据需要过滤页面内容。只需像这样用jquery请求要过滤的内容
var $response = $(data);
var response_title = $response.filter(".title").html(); //I'm assuming you are trying to pull just the title (perhaps an < h1 > tag, in this little example, from the entire external page!
$.ajax({
type: "GET",
url: "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData +"#"})
.done(function(response) {
var $response = $(response);
var response_title = $response.filter(".title").html();
$("#testDIV").html(response_title);
});
使用done函数,基于,可以使用此格式
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
所以你的最终代码可能看起来像这样
var $response = $(data);
var response_title = $response.filter(".title").html(); //I'm assuming you are trying to pull just the title (perhaps an < h1 > tag, in this little example, from the entire external page!
$.ajax({
type: "GET",
url: "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData +"#"})
.done(function(response) {
var $response = $(response);
var response_title = $response.filter(".title").html();
$("#testDIV").html(response_title);
});
我喜欢你的答案。它使用现有的代码,只需稍加修改
我会把它压缩成这样一行:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+
occasionData+"&relationship="+ forData +"#",
success: function (response) {
$("testDIV").html($(data).filter(".title").html());
}
});
我喜欢你的答案。它使用现有的代码,只需稍加修改
我会把它压缩成这样一行:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+
occasionData+"&relationship="+ forData +"#",
success: function (response) {
$("testDIV").html($(data).filter(".title").html());
}
});
您显示的代码实际上是正确的。 问题来自您的服务器提供的内容 您在这里所做的是通过AJAX调用获取整个页面,并用整个页面替换一个div的内容 服务器不应为该调用呈现整个页面,而应仅呈现要替换的div的内容。如果您使用的是rails或symfony之类的框架,那么它们通常提供一种简单的方法来检测查询是正常的GET请求还是AJAX调用 基本上你有4种策略可供选择:
您显示的代码实际上是正确的。 问题来自您的服务器提供的内容 您在这里所做的是通过AJAX调用获取整个页面,并用整个页面替换一个div的内容 服务器不应为该调用呈现整个页面,而应仅呈现要替换的div的内容。如果您使用的是rails或symfony之类的框架,那么它们通常提供一种简单的方法来检测查询是正常的GET请求还是AJAX调用 基本上你有4种策略可供选择:
错误在您发布的代码之外。您是否使用
作为运行此代码的触发器?断开的URL?为什么在ht.tp
上有点@J0HN他正在使用ajax获取“整个页面”,正如ajax所希望的那样。他应该尝试.load
从检索到的页面中获取某个div。@好的,这不会改变任何事情,他使用的代码无论如何都不应该重新加载页面。错误在外部某处。@J0HN据我所知:它没有重新加载。简单地说,div将获得整个页面。相反,他希望它只包含加载页面上特定div的内容但问题是整个页面正在加载
。也就是说,他插入数据的div正在获取整个获取的页面