Node js ajax post request

General Tech Bugs & Fixes 3 years ago

4.49K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

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 AJAX NODE JS CODE

$("#submit_btn").click(function() {
    $.ajax({
        url: '/process_add',
        type: 'POST',
        dataType: "json",
        data: $('#process_form').serializeArray(),
        success:function(data) {
            alert('done');
        },
        error: function(errorThrown,data) {
            alert('Error');
        }
    });
});


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();
        });
    });
});
0 views
0 shares

profilepic.png
manpreet 3 years ago

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 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum