jquery - NodeJS、Express 显示来自 AJAX POST 的 HTML 页面响应

标签 jquery html node.js ajax express

将 NodeJS 与 Express 服务器结合使用,当您转到 http://localhost:(Serverport) 时,NodeJS 服务器使用以下代码发送 HTML 文件进行响应:

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

现在我使用 JQuery AJAX POST 向服务器发送信息,并根据服务器响应将用户发送到 HTML 页面“/index”,或者如果用户凭据不好,则发送 HTML 页面“/loginno”。问题是 AJAX 不能很好地处理 sendfile 的服务器响应,就像服务器获取函数响应一样。

我从服务器获取文件,并在控制台中打印出完整的 HTML,但我只是不知道如何使浏览器真正转到该页面,就像使用服务器 GET 响应一样。

这是我的 Ajax 函数,它可以工作并从服务器获取 HTML 页面对象,但浏览器只是不导航到该页面。

$.ajax({
        type: "POST",
        url: '/index', //A string containing the URL to which the request is sent.
        timeout: 2000,

        //A plain object or string that is sent to the server with the request.
        data: userdata,

        //The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
        dataType: 'html',

        success: function(response,status,xhr) {
            //show content
            console.log('Success!' + response+', Status: '+status+', xhr: '+xhr)
        },
        error: function(jqXHR, textStatus, err) {
            //show error message
            alert('text status '+textStatus+', err '+err)
        }
    });

所以问题是,如何告诉 JQuery AJAX POST 函数导航到从 NodeJS 服务器响应发送的 HTML 页面对象?

最佳答案

看了这个问题给出的答案后...... Calling Response.redirect through Ajax

我能够通过使用 url 变量构建 json 响应并在 ajax 成功后,测试服务器是否可以转到新页面并使用 window.location.href 导航到那里来完成我需要的操作。

在我的 NodeJS 服务器路由中...

app.post('/index', function(req, res) { 
    //Do some req verification stuff here

    //If req verfiication passes
    var servResp = {};
    servResp.success = true;
    servResp.redirect = true;
    servResp.redirectURL = "http://localhost:3000/index";
    res.send(servResp);  
});

和我的 AJAX Post 函数...

$.ajax({
    type: "POST",
    url: '/index', //A string containing the URL to which the request is sent.
    timeout: 2000,

    //A plain object or string that is sent to the server with the request.
    data: userdata,

    //The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
    dataType: 'json',

    success: function(response,status,xhr) {
        //show content
        console.log('Success!' + response+', Status: '+status+', xhr: '+xhr)

            if(response.redirect) {
                window.location = response.redirectURL;
            }

    },
    error: function(jqXHR, textStatus, err) {
        //show error message
        alert('text status '+textStatus+', err '+err)
    }
});

感谢所有提供帮助的人!

关于jquery - NodeJS、Express 显示来自 AJAX POST 的 HTML 页面响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46510661/

相关文章:

javascript - 重新加载 rails 中的部分内容时范围困惑

javascript - 如何以位置绝对值打印到 Canvas

css - 点击显示 - 平板电脑/手机

php - 选项从数据库获取数据

javascript - 如何在 node.js 中先加载 CSS 并表达?

javascript - 找不到 .ejs 模板中的 href 和 src 属性

javascript - 确定 promise 需要多长时间

javascript - 如何将记录数与 jquery ui 选项卡名称一起显示为带计数器的字母形状?

Javascript 继承和对象字面量

javascript - 将字符串映射到javascript中的函数