user1461607
user1461607

Reputation: 2760

ajax not working in chrome

So I have this script which posts some text using ajax:

<?php
if (isset($_POST['q'])) {
    echo 'q is '.$_POST['q'];
} else {
?>
    <!DOCTYPE HTML>
    <html>
        <head>
            <script>
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open("POST","aj.php",true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlhttp.setRequestHeader("Accept","text/html");
                xmlhttp.send("q=some text");
                xmlhttp.onreadystatechange=function(){
                  if(xmlhttp.readyState===4&&xmlhttp.status===200)
                    if (r=xmlhttp.response||xmlhttp.responseText) 
                        document.write(r);
                    else 
                        alert("no response")
                }
            </script>
        </head>
        <body>
            body
        </body>
    </html>
<?php } ?>

The output is suppose to be 'q is some text' but in Google Chrome (Windows) it runs repeatedly and all you see is the word 'body' repeating across the page. Whats going wrong?

Upvotes: 0

Views: 245

Answers (1)

Sam
Sam

Reputation: 2970

jQuery is the way

$.post('ajax.php','q=some text',function(data){
    $(document.body).html(data);
 });

Upvotes: 3

Related Questions