如何从ajax响应中呈现html页面代码?

时间:2016-10-17 20:37:52

标签: javascript ajax node.js

我正在实施JavaScript身份验证,我想在用户成功登录后打开一些html文件。 当用户输入用户名和密码时,我会向服务器发送ajax请求,我将验证用户的凭据。如果验证成功,我会向注册路线发送另一个ajax请求("仪表板")。作为ajax请求的响应,我有整个html页面代码。如何在注册路径(我的情况下的仪表板)中呈现此代码?

这是我的代码

客户端:

$.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);            
            }
        });                 
    }
});  

服务器:

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

1 个答案:

答案 0 :(得分:1)

使用此:

document.location.href = '/dashboard';

在您的具体案例中:

$.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 */
    }
});