jquery - Javascript Function Not Executed Correctly -
i have app uses html & javascript, , isnt working correctly. video describes bug:
http://vk.com/video170263591_169020196
and code goes this:
function load() { $("#load").show(0,function(){ console.log('spinner loaded'); }); } function unload() { $("#load").hide(); console.log('load ended'); } function server(datasend) { var response; var posturl = "http://mailsnitch.ipx-il.com/get/index.php"; $.ajax({ type: 'post', url: posturl, datatype: 'json', timeout:4000, async: false, data: datasend, beforesend : load(), success: function(valueable){ console.log('success'); response = valueable; console.log(valueable); unload(); }, error: function(jqxhr, exception) { console.log('error'); if (jqxhr.status === 0) { alert('not connect.\n verify network.'); } else if (jqxhr.status == 404) { alert('requested page not found. [404]'); } else if (jqxhr.status == 500) { alert('internal server error [500].'); } else if (jqxhr.status == 502) { alert('bad gateway [502].'); } else if (exception === 'parsererror') { alert('requested json parse failed.'); } else if (exception === 'timeout') { alert('time out error.'); } else if (exception === 'abort') { alert('ajax request aborted.'); } else { alert('uncaught error.\n' + jqxhr.responsetext); } unload(); }, }); homecoming response; }
and log in function:
function login(redirect) { load(); if(redirect) { var email = escapehtml(document.getelementbyid("login_email").value); var password = escapehtml(document.getelementbyid("login_password").value); } else { if(localstorage.getitem("email") == null) document.location.href = 'login.html'; var email = escapehtml(localstorage["email"]); var password = escapehtml(localstorage["password"]); } if(email != "" && password != "") { if(validateemail(email)) { var valuable = server('login=&email='+email+'&password='+password);
the problem is: when loading "login" function, load function should run, isnt running. though functions order is:
load()
server() { before: load(); , ajax... }
within console, can see "load successful" shows @ same milisecond "ajax success", means load waiting ajax load before doing function.
thx helpers.
you're calling function immediately, not referencing it, should be
$.ajax({ type: 'post', url: posturl, datatype: 'json', timeout:4000, async: false, data: datasend, beforesend : load, ....
adding parenthesis calls function , returns result, undefined
, $.ajax
phone call sees.
also note setting async : false
defeats entire purpose of asynchronous javascript , xml, made synchronous instead.
edit
your code should more this
function load() { $("#load").show(); } function unload() { $("#load").hide(); } function server(datasend) { homecoming $.ajax({ type : 'post', url : "http://mailsnitch.ipx-il.com/get/index.php", datatype : 'json', timeout : 4000, info : datasend, beforesend : load }); } function login(redirect) { if(redirect) { var email = escapehtml(document.getelementbyid("login_email").value); var password = escapehtml(document.getelementbyid("login_password").value); } else { if(localstorage.getitem("email") == null) { document.location.href = 'login.html'; } var email = escapehtml(localstorage["email"]); var password = escapehtml(localstorage["password"]); } if(email != "" && password != "") { if(validateemail(email)) { server({login : '', email: email, password: password}).done(function(valuable) { // utilize valuable here }).always(unload); } } }
javascript jquery ajax
No comments:
Post a Comment