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 StackOverflow. 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({
'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);
}
});
AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.
When you make an AJAX request, your browser sends an HTTP request to a given address. The server on the other end of the request responds, and returns the data to your browser. This is the same thing that happens when you navigate to a new web page.
If your response includes the <head>
and <body>
tags:
$("html").html(response);
.
If not, and it's only content, then do:
$("body").html(response);
.
if the response is the html content, you can use this line of code:
...
'success': function(response)
{
$("body").html(response);
}
...
update:
this will be hard to replace the html tag, but what you can do:
...
'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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With