Node js ajax post request

后端 未结 2 860
花落未央
花落未央 2021-01-22 05:08

It except an error with Node js and AJAX POST, The Node request cannot check if form data are correctly handled, always it drop to error function, any solution to check below A

相关标签:
2条回答
  • 2021-01-22 05:42

    Your AJAX request '/process_add' will point to the root of your URL.Say for example abc.com is your domain the endpoint here would be 'abc.com/process_add'. Ensure your router exists in the root reference of '/' in app.Something like this

    app.js

    var index = require('./routes/index');    
    app.use('/',index);
    

    routes/index.js

        router.post('/process_add', function(req, res, next) {
    
            var item = {
                name: req.body.name,
                content:req.body.content
            };
    
            mongo.connect(url, function(err, db) {
                assert.equal(null, err);
                db.collection('data_collection').insertOne(item, function(err, result) {
                    assert.equal(null, err);
                    console.log('Process added');
                    db.close();
                });
            });
        });
    

    Happy Coding!

    0 讨论(0)
  • 2021-01-22 05:59

    You need to add the ('/') root for that api to Ajax request .post has (/process_add) hints not root for that api so if index is root ('index/process_add)

    0 讨论(0)
提交回复
热议问题