Hi,

I am currently learning AJAX, and I want to write an simple example program (it is only for practice, it has no real meaning).

The program sends request to to PHP and gets response from it, then it should write the response using message box, all should happen on page load.
I wrote the following code:

function init(){
    myRequst = new XMLHttpRequest();                          
    var url = "http://localhost/dummy.php";
    myRequst.open("GET",url,false);   
    myRequst.send(null);
    var data = myRequst.responseText;
    alert(data);
}

window.onload = init;

My PHP:

<?php
echo "Hello World";
?>

I don't see any message box when the page loads.
When using it after the page loaded with other events (not onLoad), it works and I see the message box with the response.

Can you please asisst me here.
How can I get the data on page load using XMLHttpRequest?

Thanks in advance :)

Recommended Answers

All 6 Replies

Your AJAX request has not yet been processed yet, use the following:

function init(){
 myRequst = new XMLHttpRequest();                          
 var url = "http://localhost/dummy.php";
 myRequst.open("get",url);   
 myRequst.send(null);
 ajax.onreadystatechange=function(){
 // If everything is OK:
 if ( (myRequst.readyState == 4) && (myRequst.status == 200) ) {
   // Returns the value in a alert box
   var data = myRequst.responseText;
   alert(data);
 }
}
window.onload = init();

~G

Thank you.

I tried the code you attached. Unfortunately it didn't work.
I didn't get the message box.
I even comment the if statement inside the onreadystatechange function, but still no message box.

Can you tell me please what is missing?

Sorry i forgot to add a accolade:

function init(){
 myRequst = new XMLHttpRequest();                          
 var url = "http://localhost/dummy.php";
 myRequst.open("get",url);  
 myRequst.send(null);
 myRequst.onreadystatechange=function(){
 // If everything is OK:
 if ( (myRequst.readyState == 4) && (myRequst.status == 200) ) {
   // Returns the value in a alert box
   var data = myRequst.responseText;
   alert(data);
 }
 }
}
window.onload=init();

Tried the new code, still no message box :(

I am such an idiot, I changed the || into &&, this must work, else I give up. Create these two files in the same folder, and see wether it works:

ajtest.html

<html>
<head>
<script type="text/javascript">
function init(){
 myRequst = new XMLHttpRequest();                          
 var url = "number.txt";
 myRequst.open("get",url);  
 myRequst.send(null);
 myRequst.onreadystatechange=function(){
 // If everything is OK:
 if ( (myRequst.readyState == 4) || (myRequst.status == 200) ) {
   // Returns the value in a alert box
   var data = myRequst.responseText;
   alert(data);
 }
 }
}
window.onload=init();
</script>
</head>
<body>
</body>
</html>

And number.txt:

1

~G

I finally solved it :)

The following code works:

window.onload = function(){
    var myRequst = new XMLHttpRequest();
    var url = "http://localhost/dummy.php";
    myRequst.open('get', url);
    myRequst.onreadystatechange = function(){
        if ((myRequst.readyState == 4) && (myRequst.status == 200)){
            alert(myRequst.responseText);
        }
    }
    myRequst.send(null);
}

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.