javascript 如何在不刷新整个页面的情况下重新加载 DIV 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15495967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:58:56  来源:igfitidea点击:

how to reload DIV content without refreshing whole page

javascriptjqueryhtmlajax

提问by

I want to only replace the DIV content with the content i get. After i make a get request to the server using ajax.

我只想用我得到的内容替换 DIV 内容。在我使用 ajax 向服务器发出 get 请求之后。

    $.ajax({
    type: "GET",
    url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ 
    occasionData     +"&relationship="+ forData +"#",

    success: function () {

        $("#testDIV").load();
    }
});

"testDIV" is the id of the div with which i want to replace the content got from the server.

“testDIV”是我想要替换从服务器获取的内容的 div 的 id。

采纳答案by George

   $.ajax({
type: "GET",
url: "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ 
occasionData     +"&relationship="+ forData +"#",

success: function (response) {

    $("#testDIV").html(response);
}
});

回答by George

If you're looking to fill the <div>with what is returned from your script:

如果您想<div>用脚本返回的内容填充:

$.ajax({
    type: "GET",
    url: "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ 
    occasionData     +"&relationship="+ forData +"#",

    success: function (data) {

        $("#testDIV").html(data);
        //Puts response data inside <div id="testDIV"></div>
    }
});

Oh and please note the full stop within the http://prefix. Unless your using a new protocol not known to many of us - you'll want that gone.

哦,请注意http://前缀中的句号。除非您使用我们许多人都不知道的新协议 - 您会希望它消失。

回答by Nelson

Use the first argument of success handler which carries the content, and replace your div content with it using the .html()function:

使用包含内容的成功处理程序的第一个参数,并使用以下.html()函数替换您的 div 内容:

success: function (data) {
    $("#testDIV").html(data);
}

回答by Jeremy Gross

here is the right success function :

这是正确的成功函数:

success: function(htmlFromServer) {
     $("#testDIV").html(htmlFromServer);
}

回答by Setrákus Ra

The simplest way to get this done is by using the .loadfunction

完成此操作的最简单方法是使用该.load函数

var url = "http://127.0.0.1:8000/result/?age="+ ageData +"&occasion=" + occasionData     +"&relationship="+ forData +"#";
$("#testDIV").load(url);

回答by Ihor Patychenko

Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and indicate whether it is a temporary or permanent condition. Likewise, user agents should display any included entity to the user. These response codes are applicable to any request method.

以数字“5”开头的响应状态代码表示服务器知道它遇到了错误或无法执行请求的情况。除了响应 HEAD 请求时,服务器应包含一个实体,其中包含对错误情况的解释,并指出它是暂时的还是永久的情况。同样,用户代理应该向用户显示任何包含的实体。这些响应代码适用于任何请求方法。

回答by K D

 var url = "ht.tp://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ 
    occasionData+"&relationship="+ forData +"#";
    $('#testDIV').load(url);

回答by Edwin Alex

Try this,

试试这个,

success: function (data) {
    $("#testDIV").html(data);
}