Tutorial :Make a YUI asyncRequest call which is not async



Question:

I want to make a YAHOO.util.Connect.asyncRequest call, which not is async. Just like open(method, url, async) where false is passed by async.

I can't find a "syncRequest" in the Connect class. Is this possible using YUI 2?

I tried without YUI instead:

function createRequestObject() {      var ro;      // Mozilla, Safari,...      if (window.XMLHttpRequest) {          ro = new XMLHttpRequest();          if (ro.overrideMimeType) {              ro.overrideMimeType('text/xml');              // See note below about this line          }          // IE      } else if (window.ActiveXObject) {          try {              ro = new ActiveXObject("Msxml2.XMLHTTP");          } catch (e) {              try {                  ro = new ActiveXObject("Microsoft.XMLHTTP");              } catch (e) {}          }      }      if (!ro) {          alert('Giving up :( Cannot create an XMLHTTP instance');          return false;      }      return ro;  }  function sndReq(param,server,handler) {      http = createRequestObject();      http.open('GET', server+"?"+param, false);      http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');      http.onreadystatechange = handler;      http.send(null);  }  

But in FireFox and Safari the callback function (handler) is not called on 'onreadystatechange' when false is passed in 'open'? In IE and Opera it works ok.


Solution:1

YUI3 supports synchronous requests in "YUI io" and is available on development master on GitHub. The implementation syntax is included in the README file, and will be formally documented for release in 3.1.0. This enhancement is documented as 2528181 on yuilibrary.com.

If you are in a position to use YUI 3, give io a try.


Solution:2

YUI2 doesn't support synchronous requests. According to one of the devs in IRC it will eventually be part of YUI3. They aren't in a huge rush to support it though due to the havoc it plays with the user experience.


Solution:3

Use JQuery. jQuery.ajax({async:false}) works in both IE and FF.


Solution:4

Personally I would never make sync request. AJAX is for async requests. If the request takes longer then UI will freeze.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »