javascript - Returning Response in jquery ajax function -


getting problems in response.d , based on result returning checkusers() function saving values. if entered name in in database should "user exists", if not in database should create new record.

but not getting correct value (response), observed console.log(response.d) giving me correct values 'true' or 'false'. tried know like-

  1. changing async:"false"
  2. var jqxhr = $.ajax({ , returning jqxhr.responsetext

but none of worked me . please me this.

submithandler: function (form) {          var txtname = $("#txtname").val();         var txtemail = $("#txtemail").val();         var txtsurname = $("#txtsurname").val();         var txtmobile = $("#txtmobile").val();         var txtaddress = $("#txtaddress").val();          var obj = checkuser();         if (obj == false) {             $.ajax({                 type: "post",                 url: location.pathname + "/savedata",                 data: "{name:'" + txtname + "',surname:'" + txtsurname + "',email:'" + txtemail + "',mobile:'" + txtmobile + "',address:'" + txtaddress + "'}",                 contenttype: "application/json; charset=utf-8",                 datatype: "jsondata",                 async: "true",                 success: function (response) {                      $(".errmsg ul").remove();                     var myobject = eval('(' + response.d + ')');                     if (myobject > 0) {                         binddata();                         $(".errmsg").append("<ul><li>data saved successfully</li></ul>");                     }                     else {                         $(".errmsg").append("<ul><li>opppps went wrong.</li></ul>");                     }                     $(".errmsg").show("slow");                     clear();                  },                 error: function (response) {                     alert(response.status + ' ' + response.statustext);                 }               });         }         else {              $(".errmsg").append("<ul><li>user exists </li></ul>");             $(".errmsg").show("slow");          }      }  });  $("#btnsave").click(function () {     $("#form1").submit()  }); }); 

checkusers function is:

function checkuser() {    var empname = $("#txtname").val();      $.ajax({      type: "post",     url: location.pathname + "/userexist",     data: "{name:'" + empname + "'}",     contenttype: "application/json; charset=utf-8",     datatype: "jsondata",     async: "true",     success: function (response) {         console.log(response.d);      },     error: function (response) {         alert(response.status + ' ' + response.statustext);       }    }); } 

just because database returns true or false doesn't mean gets returned checkuser().

there several options here:

  1. either make local variable in checkuser, make ajax call synchronous, set local variable response.d in success function , return local variable.

  2. another option work deferred objects , make submithandler ajax call wait checkuser ajax call return;

  3. a third option call create ajax call success callback in checkuser ajax call if user isn't created yet.

i recommend either option 2 or 3, because option 1 not userfriendly.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -