Crossbrowser Ajax - PRB: .responseText

P

par7133

Hi,
Yea, XMLHttpRequest give out a crossbrowser problem managing the
status change of the connection.

Here the solution:

var xmlhttp;
xmlhttp=null;



xmlhttp=new XMLHttpRequest();



if (xmlhttp!=null)
{
if (document.all) {
//THE PROBLEM



xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,false);
xmlhttp.send(null);



function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
response = xmlhttp.responseText;
}
else
{
// Problem retrieving data
}
}
}



if (response!="") {
// JSON code to manage the response

}



} else {



xmlhttp.open("GET",url);
xmlhttp.send(null);
xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
response = xmlhttp.responseText;

if (response!="") {
// JSON code to manage the response
}

}
else
{
// Problem retrieving data
}
}
}



}

}
else
{
// Problem with XMLHTTP
}


Hope this helps,

http://blog.daniele.bonini.name/index.php/2008/08/27/crossbrowser-ajax-prb-responsetext/
 
T

Thomas 'PointedEars' Lahn

par7133 said:
Yea, XMLHttpRequest give out a crossbrowser problem managing the
status change of the connection.

Utter nonsense. The problem with this code, which mislead you or its author
into using error-prone object inference[1], is that there is a function
declaration within a block statement which is no longer supported by
mozilla.org JavaScript[tm] as it is a proprietary feature (which is
supported by Microsoft JScript). The branching is unnecessary if you use a
function expression always:

xmlhttp.onreadystatechange = function() {
// ...
};

That is easier to see if you reduce the number of empty lines and employed
proper indentation. Besides, your code is still far from being
cross-browser or working at all (the correct order is: open,
onreadystatechange, send; and that is not the end of it). In fact, this
problem has already been solved, far better, years ago.


HTH

PointedEars
___________
[1] <http://PointedEars.de/scripts/test/whatami#inference>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top