Login | Register | FAQ
Anonymous

AJAX Example Not Working?

+ Post a reply

9 posts Page 1 of 1


AJAX Example Not Working?

by tatertat » Mon Jan 23, 2012 10:33 pm

The following code was given in the Making AJAX example...
Code: Select all
      <!DOCTYPE html>
      <html>
      <head>
      <title>Lesson 18: Making AJAX Calls</title>
      
      </head>
      <body>
      <h1>Lesson 18: Making AJAX Calls - Plain Text Response</h1>
      
      <div>
      
      <h2 id="myHeader">Click the button to call your data</h2>
      
      <input type="button" value="Click Me!" onclick="getText('lesson18_test.txt')" />
      
      </div>
      
      <script type="text/javascript">
      
      //Prepare the global variable for the request
      
      var myRequest;
      
      //Write the getText(url) function
      
      function getText(url)
      
      {
      
      //check support for the XMLHttpRequest object
      
      if (window.XMLHttpRequest)
      
      {
      
      myRequest = new XMLHttpRequest();
      
      }
      
      //else, create an ActiveXObject for IE6
      
      else
      
      {
      
      myRequest = new ActiveXObject("Microsoft.XMLHTTP");
      
      }
      
      //Call the open() method to make the request
      
      myRequest.open("GET", url, true);
      
      //Send the request
      
      myRequest.send(null);
      
      //Assign the getData() function to the
      
      //onreadystatechange property to handle server response
      
      myRequest.onreadystatechange = getData;
      
      }
      
      
      
      /**********************************************/
      
      
      
      //This function handles the server response
      
      function getData()
      
      {
      
      //Get a reference to the header element where
      
      //the returned result will be displayed
      
      var myHeader = document.getElementById("myHeader");
      
      //Check the response is complete
      
      if (myRequest.readyState ===4)
      
      {
      
      //Check the status code of the response is successful
      
      if (myRequest.status === 200)
      
      {
      
      //Store the response
      
      var text = myRequest.responseText;
      
      //Assing the returned text to the nodeValue
      
      //property of the header element (you can also use
      
      //innerHTML here if you feel it simplifies your task)
      
      myHeader.firstChild.nodeValue = text;
      
      }
      
      }
      
      }
      
      </script>
         
      </body>
      </html>



I created a file called lesson18_test.txt in the same location as the html file that just has one line that says, "Hello, World." When I click the button, it's supposed to retrieve "Hello, World" but it won't do anything. The code is taken directly from the example. Why am I not getting the right results here??? Please help!
User avatar

tatertat

  • Posts: 7
  • Joined: Mon Jan 16, 2012 2:11 am

Re: AJAX Example Not Working?

by atik » Tue Jan 24, 2012 8:28 am

Hi tatertat,
Yes you are right. The example is not working on my pc in any folder. Then i put it in the localhost in my pc and it's worked. So you have to put the html and text file in any server, then you will get it worked.
User avatar

atik

  • Posts: 471
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: AJAX Example Not Working?

by mariaantonietta » Tue Jan 24, 2012 12:20 pm

Hi tatertat,

Atik is absolutely right. As I said in lesson 18, AJAX is all about client/server communication. Just upload your files on a server and see how it goes.

tatertat wrote:The following code was given in the Making AJAX example...
Code: Select all
      <!DOCTYPE html>
      <html>
      <head>
      <title>Lesson 18: Making AJAX Calls</title>
      
      </head>
      <body>
      <h1>Lesson 18: Making AJAX Calls - Plain Text Response</h1>
      
      <div>
      
      <h2 id="myHeader">Click the button to call your data</h2>
      
      <input type="button" value="Click Me!" onclick="getText('lesson18_test.txt')" />
      
      </div>
      
      <script type="text/javascript">
      
      //Prepare the global variable for the request
      
      var myRequest;
      
      //Write the getText(url) function
      
      function getText(url)
      
      {
      
      //check support for the XMLHttpRequest object
      
      if (window.XMLHttpRequest)
      
      {
      
      myRequest = new XMLHttpRequest();
      
      }
      
      //else, create an ActiveXObject for IE6
      
      else
      
      {
      
      myRequest = new ActiveXObject("Microsoft.XMLHTTP");
      
      }
      
      //Call the open() method to make the request
      
      myRequest.open("GET", url, true);
      
      //Send the request
      
      myRequest.send(null);
      
      //Assign the getData() function to the
      
      //onreadystatechange property to handle server response
      
      myRequest.onreadystatechange = getData;
      
      }
      
      
      
      /**********************************************/
      
      
      
      //This function handles the server response
      
      function getData()
      
      {
      
      //Get a reference to the header element where
      
      //the returned result will be displayed
      
      var myHeader = document.getElementById("myHeader");
      
      //Check the response is complete
      
      if (myRequest.readyState ===4)
      
      {
      
      //Check the status code of the response is successful
      
      if (myRequest.status === 200)
      
      {
      
      //Store the response
      
      var text = myRequest.responseText;
      
      //Assing the returned text to the nodeValue
      
      //property of the header element (you can also use
      
      //innerHTML here if you feel it simplifies your task)
      
      myHeader.firstChild.nodeValue = text;
      
      }
      
      }
      
      }
      
      </script>
         
      </body>
      </html>



I created a file called lesson18_test.txt in the same location as the html file that just has one line that says, "Hello, World." When I click the button, it's supposed to retrieve "Hello, World" but it won't do anything. The code is taken directly from the example. Why am I not getting the right results here??? Please help!
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm

Re: AJAX Example Not Working?

by tatertat » Tue Jan 24, 2012 5:40 pm

so both of my files are in /public_html... is that wrong?
User avatar

tatertat

  • Posts: 7
  • Joined: Mon Jan 16, 2012 2:11 am

Re: AJAX Example Not Working?

by mariaantonietta » Wed Jan 25, 2012 2:04 pm

Hi Tatertat,

I copied and pasted the above code snippet in a folder inside my local server and it worked. Is /public_html a folder on your local machine? Is it located on a server (either on the internet with a hosting company or on your local computer)? Make sure /public_html containing both your code files is on a server and try again. Also, copy and paste the code snippet above just as it is, just to make sure there are no syntax or spelling mistakes.

tatertat wrote:so both of my files are in /public_html... is that wrong?
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm

Re: AJAX Example Not Working?

by tatertat » Wed Jan 25, 2012 8:36 pm

Hi mariaantonietta,

Yes, I have a server through 000webhost and both files are in the public_html folder. The text file has one line - "Hello, World." I named the text file "test.txt." I replaced 'lesson18_test.txt' with 'test.txt' and still not getting it...
User avatar

tatertat

  • Posts: 7
  • Joined: Mon Jan 16, 2012 2:11 am

Re: AJAX Example Not Working?

by mariaantonietta » Thu Jan 26, 2012 11:23 am

Hi Tatertat,

The fact is that I can't replicate the bug. As I said, I copied and pasted the code above, placed both files (html and txt files) inside a folder, and placed the folder in the root of my server, and it worked. Would it be possible for you to send me your code, and the url where your web page is hosted. Perhaps I could make out what's going on.

tatertat wrote:Hi mariaantonietta,

Yes, I have a server through 000webhost and both files are in the public_html folder. The text file has one line - "Hello, World." I named the text file "test.txt." I replaced 'lesson18_test.txt' with 'test.txt' and still not getting it...
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm

Re: AJAX Example Not Working?

by Глеб » Thu Feb 02, 2012 10:25 pm

I have the same problem as Tatertat.
My both files are in the public_html folder but example is not working.
I think it's URL.
When I wrote -
alert( "url=" + myRequest.url);
I've got -
url=undefined
User avatar

Глеб

  • Posts: 1
  • Joined: Tue Jan 24, 2012 4:38 pm

Re: AJAX Example Not Working?

by mariaantonietta » Fri Feb 03, 2012 11:39 am

Hi, Tatertat's problem was related to his hosting company, which doesn't serve up .txt files. If you can't access the text file directly by navigating to it with your browser, you can't access it via AJAX either. Have a talk to your hosting provider, you might get your answer.

Глеб wrote:I have the same problem as Tatertat.
My both files are in the public_html folder but example is not working.
I think it's URL.
When I wrote -
alert( "url=" + myRequest.url);
I've got -
url=undefined
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm


+ Post a reply

Page 1 of 1