irynabond
irynabond

Reputation: 1067

How to render html page code from ajax response?

I am implementing JavaScript authentication and I want to open some html file when user has logged in successfully. When user enters username and password I send ajax request to the server where I'm validating user's credentials. If validation was successful I send another ajax request to the registered route ("dashboard"). As a response from ajax request I have the whole html page code. How can I render this code at the registered route (dashboard in my case)?

Here is my code

Client:

$.ajax({
    url: "signinUser", 
    method: "GET", 
    headers: {"Authorization": "Basic " + btoa(name + ":" + pass)}, 
    success: function(result) { 
        $.ajax({
            url: "dashboard", 
            method: "GET", 
            success: function(result) { 
                console.log(result);            
            }
        });                 
    }
});  

Server:

app.get('/dashboard', function (req, res) { 
    res.sendFile(path.join(__dirname+'/dashboard.html'));
});

Upvotes: 0

Views: 4674

Answers (1)

Jacopo Brovida
Jacopo Brovida

Reputation: 465

Use this:

document.location.href = '/dashboard';

In your specific case:

$.ajax({
    url: "signinUser", 
    method: "GET", 
    headers: {"Authorization": "Basic " + btoa(name + ":" + pass)}, 
    success: function(result) { 
        document.location.href = '/dashboard';
        // or
        /* document.location.href = result.url; // for dynamic redirect */
    }
});

Upvotes: 1

Related Questions