Javascript ajax请求后重新加载div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38237894/
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-08-23 21:13:31  来源:igfitidea点击:

reload div after ajax request

javascriptjqueryajax

提问by Reco Jhonatan

how i can refresh or reload a div after ajax request? i have this code:

ajax请求后如何刷新或重新加载div?我有这个代码:

function Register(event) {
event.preventDefault();
    console.log(event);
    $.ajax({
        type: 'POST',
        url: register,
        data: {
            name: $('#name').val(),
            email:   $('#Email').val(),
            password: $('#password').val(),
            _token: $('#token').val()
        },
        dataType : "json",

        success: function (data) {
           if (data.success){

             $('#header').
               $('#Register').modal('hide');

           }
        }
    });

}

i need reload or refresh the header div.

我需要重新加载或刷新标题 div。

回答by Sree KS

You can load the div like this.Please mind the space before divid (" #divid");

你可以像这样加载div。divid前请注意空格 (" #divid");

if (data.success){
   $("#divid").load(" #divid");
    }

回答by Supunsam

I know this post is old and already answered. But I would Like to add something to it. If you look at the answer of @Sree KS it will load the div on top of the existing div. Which is not ideal. Just run inspect div after the method is completed.

我知道这篇文章很旧并且已经回答了。但我想添加一些东西。如果您查看@Sree KS 的答案,它将在现有 div 之上加载 div。这并不理想。方法完成后运行inspect div。

To avoid this issue use the following code:

为避免此问题,请使用以下代码:

$("#divid").load(" #divid > *");

Hope this will help to anyone who has the same issue.

希望这对有同样问题的人有所帮助。

回答by Suman Singh

I think you are trying to reload the header section after login to show the username, logout etc. To do that you have to return the all html from the your ajax method into the "data.success" variable using like json encode.

我认为您正在尝试在登录后重新加载标题部分以显示用户名、注销等。为此,您必须使用类似 json 编码将 ajax 方法中的所有 html 返回到“data.success”变量中。

your ajax method function () {
    $response = array('success' => 'Your html data come here');
    echo json_encode($response);
    exit(0);
}