Javascript-Flash消息无法在AJAX调用中显示

时间:2018-10-02 18:29:31

标签: javascript jquery knockout.js handlebars.js connect-flash

我正在开发一个应用程序,其中有可搜索的参考数据库,并且有一个管理应用程序,允许用户编辑数据库。面向公众的一方使用Knockout JS通过RESTful API与服务器通信。管理应用程序使用快递。页面使用车把从模板渲染。编辑视图使用与公共视图相同的渲染代码。

我正在使用connect-flash将动作结果消息发送给编辑器。当表单提交到页面(用于添加新引用)时,Flash消息将按预期显示,但是,对于表视图中每个项目可用的命令(例如删除特定引用),我使用来自视图模型中定义的函数的ajax调用。呼叫可以正确执行并执行操作,但是Flash消息永远不会显示给用户。我有一个自定义中间件,可将Flash消息收集到模板的替换对象中。当我记录发送哪些消息时,我再也看不到删除调用产生的消息,因此我不仅会在刷新后丢失它。

这是我正在使用的请求处理程序:

var router = express.Router();
var client = solr.createClient(...);

router.get('/', function (req, res, next) {
    res.render('refs', req.replacements);
});

// This one is submitted via an html form post, and works
router.post('/new', function (req, res, next) {

    var doc = {};

    ... // Form reading and validation code

    client.add(doc, { commitWithin: 50 }, function (err, data) {

        if (err) {
            console.log(err);
            req.flash('error', 'A problem occurred during submit.'); //works
        } else { // Success            
            req.flash('yay', 'New reference successfully added.'); //works
        }

        res.redirect(303, '/refs?rows=1&q=id%3A' + newId);
    });
});

// This one is submitted via ajax in the view model. Action works, flash doesn't
router.delete("/:id(\\d+)", function (req, res, next) {

    var id = req.params.id;
    var query = 'q=id:' + id;


    var doc = undefined;
    client.get('refs', query, function (err, obj) {
        if (err) {
            req.flash('error', 'Unable to obtain a copy of object to delete for audit log. Reference not deleted.');
            res.redirect(303, '/refs');
        } else {
            doc = obj.response.docs[0];

            client.deleteByID(id, { commitWithin: 50 }, function (err, data) {
                if (err) {
                    console.log(err);
                    return req.flash('error', 'A problem occurred during delete submission.');
                } else { // Success
                    req.flash('yay', 'Reference successfully deleted.'); // Never seen
                }
            });
        }
    });

    res.redirect(303, url.format({
        pathname:"/refs",
        query:req.query,
    }));
});

我已经尝试将重定向更改为278,以防止浏览器透明地处理重定向,然后按照How to manage a redirect request after a jQuery Ajax call在ajax调用的成功函数中对其进行处理,但这没有帮助。

这是定义视图模型的客户端javascript:

function RefsViewModel(qString) {
    "use strict";
    var self = this;
    self.refs = ko.observableArray();

    $.ajax({
        // This makes the call which populates self.refs with observables
    });

    self.deleteRef = function (ref) {
        $.ajax({ // Makes an AJAX query to the server for the source
            url: "/refs/" + ref.id(),
            type: "DELETE",
            error: function (jqXHR) {
                console.log("ajax error " + jqXHR.status);
                alert("Error sending delete request");
            }
        });
    }
}

还有我用来收集Flash消息的中间件:

var flash = require('connect-flash');
function flashMessageCenter(req, res, next) {
    if(typeof req.replacements === "undefined") {
        req.replacements = {}
    }

    req.replacements.errorMessage = req.flash("error");
    req.replacements.yayMessage = req.flash("yay");
    req.replacements.infoMessage = req.flash("info");
    console.log(req.replacements);

    return next();
}

0 个答案:

没有答案