Any insight into the problem here? When run, the code yields nothing. No text appears on the page. If I unment the mented line, the xml results appear. Why can't I pass it as a variable? (I do get the alert, fyi, so the function is being called.)
<script type="text/javascript">
function loadXMLDoc(parameterString)
{
alert("loadXMLDoc has been called.");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("xmlResults").innerHTML = xmlhttp.responseText;
alert("Got the response!");
return xmlhttp.responseText;
}
else document.getElementById("xmlResults").innerHTML = "No results."
}
var url = ".svc?" + parameterString;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
$(function(){
//left out irrelevant code which creates the var "parameters"
var results = loadXMLDoc(parameters);
document.getElementById("xmlresults").innerHTML = results;
});
</script>
<body>
<div id="xmlResults"></div>
</body>
Any insight into the problem here? When run, the code yields nothing. No text appears on the page. If I unment the mented line, the xml results appear. Why can't I pass it as a variable? (I do get the alert, fyi, so the function is being called.)
<script type="text/javascript">
function loadXMLDoc(parameterString)
{
alert("loadXMLDoc has been called.");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("xmlResults").innerHTML = xmlhttp.responseText;
alert("Got the response!");
return xmlhttp.responseText;
}
else document.getElementById("xmlResults").innerHTML = "No results."
}
var url = "http://metpetdb.rpi.edu/metpetwebsearchIPhone.svc?" + parameterString;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
$(function(){
//left out irrelevant code which creates the var "parameters"
var results = loadXMLDoc(parameters);
document.getElementById("xmlresults").innerHTML = results;
});
</script>
<body>
<div id="xmlResults"></div>
</body>
Share
Improve this question
asked Apr 29, 2012 at 5:04
temporary_user_nametemporary_user_name
37.2k48 gold badges160 silver badges243 bronze badges
1
- Ah! Further reading has informed me that you cannot return a variable from an asynchronous call. I must use a helper function. Is this absolutely and unavoidably true? – temporary_user_name Commented Apr 29, 2012 at 5:08
2 Answers
Reset to default 4By definition, an asynchronous call performs the real work without making the caller wait for the result. You do need to use a callback function, for example:
<script type="text/javascript">
function loadXMLDoc(parameterString, onComplete, onError) {
alert("loadXMLDoc has been called.");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if(xmlhttp.status==200) {
//document.getElementById("xmlResults").innerHTML = xmlhttp.responseText;
alert("Got the response!");
onComplete(xmlhttp.responseText);
} else {
onError();
}
}
};
var url = "http://metpetdb.rpi.edu/metpetwebsearchIPhone.svc?" + parameterString;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
$(function(){
//left out irrelevant code which creates the var "parameters"
loadXMLDoc(parameters, function(results) {
// this function will be called if the xmlhttprequest received a result
document.getElementById("xmlresults").innerHTML = results;
}, function() {
// this function will be called if xhr failed
document.getElementById("xmlResults").innerHTML = "No results.";
});
});
</script>
By the way, since you are already using jQuery, you should just use its builtin AJAX functionality instead of building your custom xmlhttprequest.
For one thing, you have capitalization issues, i.e. xmlresults
vs. xmlResults
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744321028a4568413.html
评论列表(0条)