dom - XMLHttp request - wait for actual response and then add text to the page -
starting code provided filedrag.js i've implemented upload file async function.
the problem can't retrieve text once call done. i'm sure text returned, since shows in firebug console once post request done. problem code doesn't add page.
here's code:
if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } function showhint(str,action,target,url) { xmlhttp.open("post",url,true); xmlhttp.setrequestheader("enctype","multipart/form-data"); var fd = new formdata; fd.append('filexls', str); xmlhttp.send(fd); console.log(xmlhttp.status); if (xmlhttp.readystate==4 && xmlhttp.status==200) { $('#successup').html(xmlhttp.responsetext); } } i've done testing , think problem last part of code. if place console.log(xmlhttp.status) before if statement, returns 0 (firebug @ end of request returns 200), while if place inside if nothing shows in console. best guess problem if statement runs before actual request done returns false.
you need assign event listener xmlhttp.onreadystatechange.
inside listener, can check new readystate xmlhttp.readystate. readystate of 4 means completed. can access following properties: xmlhttp.status, xmlhttp.responsetext. can access headers either using xmlhttp.getallresponseheaders() or xmlhttp.getresponseheader("content-type")
if request status falsy (check if(!xmlhttp.status)) request failed. otherwise status http status code of response.
you do:
xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { $('#successup').html(xmlhttp.responsetext); } } note: can use synchronous xmlhttprequests, these block entire ui before completing, it's not practical, unless when done inside web worker. see documentation @ mdn.
also, per http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx , it's recommended use new activexobject("msxml2.xmlhttp") . microsoft.xmlhttp identifier deprecated. note article 2006!
the “microsoft” namespace older , implemented in msxml3 legacy support. it’s unfortunate used “better” name on older version, stick “msxml2” namespace when instantiating objects.
Comments
Post a Comment