Node.js through jquery Ajax to get the parameters __js

Source: Internet
Author: User

This is the experience of continuing to learn on the basis of previous exercises. After the page brushes out the data to want to perform the deletion to some content, here uses the jquery Ajax, passes through the Ajax to a unique value, like collection inside _id, then receives this parameter in the node handler, calls the Remove method to perform the deletion operation. The specific code is as follows:

1, my Ajax code is written in a page.

$ ('. My-delete '). Click (function () {
     var id=$ (this). attr ("Data-id");  In the page I direct the _id value of the data to the Data-id
     $.ajax ({
         type: ' Post ',
         URL: "/msgdelete",
         data:{id:id},
         Success:function (data) {  //In my writing, here success is not executed, ran directly to the message handler, successfully deleted over the
             if (data) {
                 Console.log ( ' Successful AH ');
             else{
                  Console.log ("return value is null");}})
 
This process URL is "/msgdelete", so you need to modify App.js add Msgdelete processing event, I am on the current page to perform the delete operation, so the Turn page is also the current page.

App.get ('/msgdelete ', Message.del);
App.post ('/msgdelete ', message.delmsg);
Modify the Del and Delmsg methods inside the mesage.js as shown below.
Exports.del = function (req,res) {
    res.render (' home ', {title: ' Delete Message '})
}

exports.delmsg = function (req,res) {
    msgmodel.remove ({_id:req.body.id},function (err,data) {
        if (!err) {
res.send (' True         //delete succeeds to return true for the data received by the success function of the front-end Ajax, where the format of the send can be defined on its own
}else{
res.send (' false ');
}
    })
}
The delmsg here is really the process of implementing the delete operation, the Remove () method of two parameters, the first is the condition, the second is the callback function, here I want to delete the specified ID of the data, through the req.body.idTo get the previous page Ajax incoming parameters, after the deletion is complete, recall the Find method to look up the data of the database to see if the result is correct, docs for the returned array column.

Specific what delete, phase to remove the hidden ah and so on do not write, the need for their own research, anyway, the return value has been given out.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.