asp.net - How can I return a value from a function that uses a jQuery ajax call? -



asp.net - How can I return a value from a function that uses a jQuery ajax call? -

i have next javascript function makes phone call asp.net webmethod returns true if username exists , false if not. next give me right reply in alert box if alter alert(result.d) homecoming result.d result of function undefined. how can function ajax phone call contained in homecoming value based on response webmethod?

function doesentereduserexist() { var valfromusernamebox = $("#txtusername").val(); var jsonobj = '{username: "' + valfromusernamebox + '"}'; $.ajax({ type: 'post', contenttype: 'application/json; charset=utf-8', data: jsonobj, datatype: 'json', async: false, url: 'addnewuser.aspx/isusernamevalid', success: function (result) { alert(result.d); }, error: function (err) { alert(err); } }); }

the thing note ajax function non blocking. specify 2 callback functions, 1 success , 1 error, 1 of these functions executed when underlying request finished.

your doesentereduserexist not wait ajax phone call complete, , thus, homecoming value of function undefined.

the proper way go on execution based on result of phone call break functionality out separate function, , phone call success function.

asp.net jquery

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -