thank_you
thank_you

Reputation: 11107

AJAX not working with XAMPP or is it just impossible

I'm still relatively new to AJAX and am trying to figure out why my simple test is not working. From what I read AJAX will not work under one domain, and other words pop up like cross-site and remote server. Anyways my problem is is that I'm not sure if my code is wrong or simply what I'm trying to do is impossible. I created a simple ajax request to submit data when I click on the button. Here is the code for the first script.

<html>
  <head>
    <script type="text/javascript" >
      function load(thediv, thefile) {
        if (window.XMLHttpRequest) {
          xmlhttp = new XMLHttpRequest();   
        } 
        else {
          xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }   
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById(thediv).innerHTML = xmlhttp.responseText;
          }     
        }
        xmlhttp.open('GET', thefile, true);
        xmlhttp.send();
      }
    </script>
  </head>
  <body>
    <input type="submit" onclick="load('adiv', 'hello.php');">
    <div id="adiv"></div>
  </body>
</html>

Here is the code for the file hello.php

<?php
  echo 'aaa';
?>

Upvotes: 0

Views: 4885

Answers (1)

Marc B
Marc B

Reputation: 360662

AJAX is just an http request done in the background. But yes, there are security restrictions that prevent you doing a normal ajax request to any arbitrary server.

What your code is missing is actually setting the URL that you're placing the request to. You've got hello.php as a parameter to your load() function, but you never actually USE it. So you're doing an AJAX request to... "nowhere".

Upvotes: 1

Related Questions