jquery - Should I use Ajax POST or GET to create an element in the DB and getting the created ID with Node.JS? -
i want add new element in database , i'd created id. (using node.js)
at moment, use ajax post add element in db can't figure how returning id.
should use or there way id post ? best practive ?
here's code samples:
client script:
ajaxpost('/newelement', element, function(iddb) {         alert(iddb); //it says undefined     });  function ajaxpost(path, data, callback) {   $.ajax('http://'+ url + path, {     type: 'post',     data: json.stringify(data),     contenttype: 'application/json',     success: function() { if ( callback ) callback(); },     error  : function(xmlhttprequest, textstatus, errorthrown) { if ( callback ) callback(); } }); } server
app.post('/newelement', function(req, res) {     querydb.insertelement(client, req.body, function(err, iddb) {          if (err) {             console.error(err);             res.end();         } else {             console.log(iddb); //i have returned id here...             res.send(iddb);         }      }); }); many thanks.
the best practice use post, ideally should never change state of data verbs other put , post. put preferred database updates, , post create new entities, so, post better on case
i suggest returning (in post) element encoded json (like get)
Comments
Post a Comment