Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不刷新的情况下将新内容加载到DIV_Javascript_Jquery_Html_Ajax_Web Deployment - Fatal编程技术网

Javascript 如何在不刷新的情况下将新内容加载到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

我想在不刷新整个页面的情况下将新内容从服务器加载到DIV。 我尝试了以下方法

$.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的内容。而不是整页
  • 向同一页面发出请求,并检测该请求是正常HTTP请求还是AJAX调用。在此基础上,返回整个页面或仅返回div的内容。您可能需要在框架/工具箱文档中查找帮助程序
  • 发出AJAX请求,但请求一个JSON对象。在客户端用HTML转换JSON以替换div的内容。这就是“我的应用程序只是一个API”的方法。这是我个人的最爱,因为这个JSON端点可以用于其他目的(例如:移动应用程序),因为它只承载内容,而不承载演示。这也是性能方面最快的方法,因为计算的很大一部分是在客户端完成的。另一方面,这需要编写更多的JS
  • 始终呈现整个页面,并仅过滤客户端上需要的内容。这是愚蠢的做法。好处是您不必修改服务器,但当只需要一个子集时,返回整个页面将浪费处理时间和带宽

  • 您显示的代码实际上是正确的。 问题来自您的服务器提供的内容

    您在这里所做的是通过AJAX调用获取整个页面,并用整个页面替换一个div的内容

    服务器不应为该调用呈现整个页面,而应仅呈现要替换的div的内容。如果您使用的是rails或symfony之类的框架,那么它们通常提供一种简单的方法来检测查询是正常的GET请求还是AJAX调用

    基本上你有4种策略可供选择:

  • 向特定端点发出请求,该端点仅用于ajax调用,并返回要替换的div的内容。而不是整页
  • 向同一页面发出请求,并检测该请求是正常HTTP请求还是AJAX调用。在此基础上,返回整个页面或仅返回div的内容。您可能需要在框架/工具箱文档中查找帮助程序
  • 发出AJAX请求,但请求一个JSON对象。在客户端用HTML转换JSON以替换div的内容。这就是“我的应用程序只是一个API”的方法。这是我个人的最爱,因为这个JSON端点可以用于其他目的(例如:移动应用程序),因为它只承载内容,而不承载演示。这也是性能方面最快的方法,因为计算的很大一部分是在客户端完成的。另一方面,这需要编写更多的JS
  • 始终呈现整个页面,并仅过滤客户端上需要的内容。这是愚蠢的做法。好处是您不必修改服务器,但当只需要一个子集时,返回整个页面将浪费处理时间和带宽

  • 错误在您发布的代码之外。您是否使用
    作为运行此代码的触发器?断开的URL?为什么在
    ht.tp
    上有点@J0HN他正在使用ajax获取“整个页面”,正如ajax所希望的那样。他应该尝试
    .load
    从检索到的页面中获取某个div。@好的,这不会改变任何事情,他使用的代码无论如何都不应该重新加载页面。错误在外部某处。@J0HN据我所知:它没有重新加载。简单地说,div将获得整个页面。相反,他希望它只包含加载页面上特定div的内容
    但问题是整个页面正在加载
    。也就是说,他插入数据的div正在获取整个获取的页面