Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace the entire html webpage with ajax response?

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);    
           }
       });
like image 757
Dillinger Avatar asked Nov 25 '15 10:11

Dillinger


People also ask

Does AJAX work with HTML?

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.

What happens when JavaScript makes an AJAX request in a browser?

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.


2 Answers

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

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

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

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

like image 174
L Ja Avatar answered Oct 16 '22 09:10

L Ja


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

like image 35
Fribu - Smart Solutions Avatar answered Oct 16 '22 09:10

Fribu - Smart Solutions