javascript - "true" value sent from $.post not being considered in the back-end -
i'm scratching head on issue deals true/false/null values, tried several variations including type coercion, false, null etc.
basically, when click on someone's profile picture, happens on front-end:
var isprofilephoto = false; if ( $(this).data('profilephoto') === true ) { isprofilephoto = true; photoid = parseint( $(that).data('photoid') ); } $.post('/mod', { isprofilephoto: isprofilephoto, photoid: photoid }, function (data) { if (data.msg === 'delete pic success') { $(that).css({ opacity: 0 }); } else { alert("there problem. please contact webmaster. error code: " + data.msg); } }); in back-end, have if/else case checks if it's profile photo, if looks like:
console.log(req.body.isprofilephoto); // true if (req.body.isprofilephoto == true) { modmodel.deletephoto(photoid, function () { if (userip !== null) { modmodel.banuserip(userip, function (response) { if (response === true) { homecoming res.send('delete pic success') } } ); } else { homecoming res.send({msg: 'delete pic success'}); } }); } else { // other stuff } however 1 time in back-end goes else case, if req.body.isprofilephoto true, goes else case...
any idea?
your code going create post request content-type application/x-www-form-urlencoded , post body looks this:
isprofilephoto=true&photoid=123 your server receives string (well, byte stream...) , unless have code or module tells otherwise, has no way know 4 bytes true should converted boolean, or 3 bytes 123 should converted number (it's probable orm takes care of latter @ point, though).
one way around send json request instead. (in jquery passing "json" 4th argument $.post). in such request content-type application/json , post body (except without whitespace):
{ "isprofilephoto": true, "photoid": 123 } in json, true boolean , "true" string, when server parses it'll automatically have right type. of course, you'd have alter server-side code parse json body, that's pretty easy these days.
javascript jquery node.js express
No comments:
Post a Comment