help me with ajax

bioshock

New Member
Messages
27
Reaction score
0
Points
0
why this javascript doesn't work. Just shows an empty dialog box
the pkg.html retruns some plain text
Code:
function get_data(url)
{
var http = new XMLHttpRequest()   ;
var result = "";
http.open("GET", url, true);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.


if(http.readyState == 4 || http.readyState == "complete") {


result=http.responseText;

    }
}
http.send(null);
return result;
}
  /*]]>*/
  var result = "";
  result=get_data("pkg.html")
  alert(result);
i am really noob in javascript please help
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You seem to have an odd mix of synchronous and asynchronous code in there, so I have made it all asynchronous:

Code:
var xmlhttp = null;
var result = "";

function get_data(url)
{
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = rec_data
xmlhttp.send(null);
}

function rec_data(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
result = xmlhttp.responseText;
alert(result);
}
}

get_data("pkg.html")
 
Last edited:
Top