jquery: If statement not working inside ajax success function -
i have success function in ajax returns response text python script can either "success" or "empty". want place if-loop inside success function, if-loop not working. getting correct data python script because alert statement working fine , printing "success". not enter ifloop
i have tried bunch of things control not entering if-loop, please tell me doing wrong:
submithandler: function (form) { $.ajax({ type: 'post', url: '/cgi-bin/getdataworld.py', data: $(form).serialize(), success: function(data) { //document.write(result); console.log("result "+data); alert(data); if(data === "success"){ window.location = 'index.html'; } else{ alert("no data present"); } }, error: function (responsedata) { console.log('ajax request not recieved!'); } }); return false; }
this means you're responding isn't "success". has line break or other whitespace before or after it, maybe more one.
perhaps:
if (/^\s*success\s*$/.test(data)) { // success } or use jquery's $.trim:
if ($.trim(data) === "success") { // success } also sure you're not replying "success" or "success", string comparisons case-sensitive.
if you're not sure:
if (/^\s*success\s*$/i.test(data)) { // success } or
if ($.trim(data).touppercase() === "success") { // success }
Comments
Post a Comment