Node js ajax post request

General Tech Bugs & Fixes 2 years ago

4.77K 2 0 0 0

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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 2 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