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

Dil*_*ger 9 html javascript php ajax jquery

在讨论具体问题之前,我需要解释几个基本步骤.

首先,该申请涉及客户在线约会的管理.

填写表格并向美容中心提供治疗并提供信息后,客户将进入确认页面.

现在,此页面执行ajax请求以在数据库上存储约会.

如果一切顺利,则会显示一个页面,其中包含成功消息的约会详细信息.

问题是页面当前仅显示在响应中,即在选项卡网络控制台浏览器中.

所以我的问题很简单: How can I replace the entire structure of the html page with actual one shown in the response?

我甚至在StackOverflow上发现了很多关于网络的问题.但所有这一切都限于对div的附加.我不需要挂起但也可以替换<html>,如何重写html页面.我不知道该怎么做,我需要你的一些建议.

为了完整性,这是回复给我ajax响应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);    
           }
       });
Run Code Online (Sandbox Code Playgroud)

L J*_* Ja 20

如果您的回复包含<head><body>标记:

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

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

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


Fri*_*ons 7

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

...
'success': function(response)
       {
           $("body").html(response);
       }
...
Run Code Online (Sandbox Code Playgroud)

更新:

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

...
'success': function(response)
       {
           $("html").html($("html", response).html());
       }
...
Run Code Online (Sandbox Code Playgroud)

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


小智 7

这个问题已经在这里处理过: Replace HTML page withcontents returned via AJAX

基本上,你可以尝试(可惜这在 IE 上不起作用):

document.open();
document.write(newContent);
document.close();
Run Code Online (Sandbox Code Playgroud)

或者,因为你正在使用jquery

$("body").html(data);
Run Code Online (Sandbox Code Playgroud)

问候