javascript - Ajax method is not working in FF -


i using following js code call web handler. code calls handler in ie , not ff.

$.ajax({ type: "get",             url: "../masterpages/ahmhandler.ashx?t=1",             datatype: "html",             success: function (msg) {                 document.getelementsbyname('cartid')[0].value = msg;                 }             ,             error: function (e) {                 return false;             }         });         sleep(2000); 

what problem code?

having seen sleep() call in code, , comment alert(), problem lack of understanding of how ajax code works.

when mak ajax call, called asynchronously. means call made, , rest of current function carries on running without stopping wait ajax code run.

the ajax success function called eventually, when http request complete. in meanwhile, current function carry on running.

the point here cannot rely on given sequence of events if have code in same function runs after ajax call made.

putting sleep() there might make appear work because browsers might see sleeping time opportunity run ajax success function, code seems run in right order. putting alert() there more make work, because alert() take more time before cleared, ajax function has more chance run.

but should not rely on either of them execution sequence right.

what should instead put code want run after ajax call inside success function. way sure run after ajax call finished.

hope helps.

[edit] further clarification after op's comment:

spudley, sleep function own function keep browser being redirect 2 secs. function sleep(milliseconds) { var start = new date().gettime(); (var = 0; < 1e7; i++) { if ((new date().gettime() - start) > milliseconds) { break; } } }

just ref, sleep function terrible idea in javascript. should use settimeout() kind of thing.

however, point still same -- have code runs after $.ajax(), , blocking execution of ajax success function. if you're doing redirect right afterward, success function never gets chance run.

an alert() indeed make work because success function find slot run when alert cleared, before sleep called, shouldn't rely on that.

the answer remains same: should put code want run after ajax call inside success function.

here's code changes made:

$.ajax({ type: "get",         url: "../masterpages/ahmhandler.ashx?t=1",         datatype: "html",         success: function (msg) {             document.getelementsbyname('cartid')[0].value = msg;             settimeout(function() { //this instead of sleep function                  //this need redirect, or whatever else you're doing after ajax completes.             }, 2000);         }         ,         error: function (e) {             return false;         } }); //don't put **any** code here after ajax call! put in success function. 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -