如何用ajax响应替换整个html网页?

时间:2022-08-29 19:12:47

Before going to the specific question I need to explain a few basic steps.
First, the application in question deals with the management of appointments online by customers.
The customer after filling a form with the treatments for the beauty center and providing their information comes to the confirmation page.
Now this page performing an ajax request to store the appointment on the database.
If everything is successful is shown a page containing the details of the appointment with the success message.
The problem is that the page is currently displayed only in the response, that is, in the tab network console browser.
So my question is simple: How can I replace the entire structure of the html page with actual one shown in the response?
I found a lot of questions on the web even on *. But all of this is limited on an append to a div. I do not need to hang but also replace the <html>, how to rewrite the html page. I have no idea how to do this and I need some advice from you.
For completeness, this is the code that comes back to me ajax response html:

在讨论具体问题之前,我需要解释几个基本步骤。首先,该申请涉及客户在线约会的管理。客户填写表格并提供美容中心的护理并提供信息后,将进入确认页面。现在,此页面执行ajax请求以在数据库上存储约会。如果一切顺利,则会显示一个页面,其中包含成功消息的约会详细信息。问题是页面当前仅显示在响应中,即在选项卡网络控制台浏览器中。所以我的问题很简单:如何用响应中显示的实际结构替换html页面的整个结构?我甚至在*上发现了很多关于网络的问题。但所有这一切都限于附加到div。我不需要挂起但也可以替换,如何重写html页面。我不知道该怎么做,我需要你的一些建议。为了完整性,这是回复给我的代码ajax response html:

$.ajax({
          'type': 'POST',
          'url': 'backend_api/ajax_save_appointment',
          'data': postData,
          'success': function(response)
           {
               console.log(response);
           },
           'error': function(jqXHR, textStatus, errorThrown)
           {
             console.log('Error on saving appointment:', jqXHR, textStatus, errorThrown);    
           }
        });

3 个解决方案

#1


13  

If your response includes the <head> and <body> tags:

如果您的回复包含和标记:

$("html").html(response);.

$( “HTML”)。HTML(响应);.

If not, and it's only content, then do:

如果没有,而且它只是内容,那么:

$("body").html(response);.

$( “身体”)。HTML(响应);.

#2


5  

if the response is the html content, you can use this line of code:

如果响应是html内容,则可以使用以下代码行:

...
'success': function(response)
       {
           $("body").html(response);
       }
...

update:

更新:

this will be hard to replace the html tag, but what you can do:

这将很难替换html标签,但你可以做什么:

...
'success': function(response)
       {
           $("html").html($("html", response).html());
       }
...

you parse the response with jquery, getting the content of html and replacing the content of the current html

你用jquery解析响应,获取html的内容并替换当前html的内容

#3


0  

this question has already been treated here: Replace HTML page with contents retrieved via AJAX

此问题已在此处理:将HTML页面替换为通过AJAX检索的内容

Basically, you may try (My bad this is not working on IE):

基本上,你可以尝试(我的坏,这不适用于IE):

document.open();
document.write(newContent);
document.close();

or, since you are using jquery

或者,因为你正在使用jquery

$("body").html(data);

Regards

问候

#1


13  

If your response includes the <head> and <body> tags:

如果您的回复包含和标记:

$("html").html(response);.

$( “HTML”)。HTML(响应);.

If not, and it's only content, then do:

如果没有,而且它只是内容,那么:

$("body").html(response);.

$( “身体”)。HTML(响应);.

#2


5  

if the response is the html content, you can use this line of code:

如果响应是html内容,则可以使用以下代码行:

...
'success': function(response)
       {
           $("body").html(response);
       }
...

update:

更新:

this will be hard to replace the html tag, but what you can do:

这将很难替换html标签,但你可以做什么:

...
'success': function(response)
       {
           $("html").html($("html", response).html());
       }
...

you parse the response with jquery, getting the content of html and replacing the content of the current html

你用jquery解析响应,获取html的内容并替换当前html的内容

#3


0  

this question has already been treated here: Replace HTML page with contents retrieved via AJAX

此问题已在此处理:将HTML页面替换为通过AJAX检索的内容

Basically, you may try (My bad this is not working on IE):

基本上,你可以尝试(我的坏,这不适用于IE):

document.open();
document.write(newContent);
document.close();

or, since you are using jquery

或者,因为你正在使用jquery

$("body").html(data);

Regards

问候