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

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

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

客户填写表格并提供美容中心的护理并提供信息后,将进入确认页面。

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

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

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

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

我甚至在StackOverflow上发现了很多关于网络的问题。 但所有这一切都限于附加到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); } }); 

如果您的回复包含标记:

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

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

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

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

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

更新:

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

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

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

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

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

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

或者,因为你正在使用jquery

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

问候