5
Answers

Ajax does not work !!!

Majid Kamali

Majid Kamali

13y
3k
1
Hi.
I was learning ajax from w3schools.com and copied one of it's examples and paste it to a notepad, then saved it with html extension.
then I changed file names (url that will be retrieved) to one of text files in that folder.
But it creates "Access is denied" error in IE. FireFox does not even execute it.
What's the problem?
Thanks
Answers (5)
0
Ravishankar Singh
NA 3.4k 458.3k 13y
Hi Majid,
Problem is here:

xmlhttp.open("GET","textFile.txt",false);

When you are calling "textFile.txt",its not finding the url from web.
for your more understanding.You can give any url and the file path e.g "http://sharepointwithravi.blogspot.com/p/about-me.html".
Otherwise do the following:
1.Create an Asp.net project.
2.In your default.aspx page should look like below code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxPage.aspx.cs" Inherits="AjaxPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My Ajax Page</title>
    <script type="text/javascript">
        function loadXMLDoc() {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "textFile.txt", false);
            xmlhttp.send();
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
    </form>
</body>
</html>
3.Add new Item ,Select Textfile.Named as textFile.txt.
4.Run your code now.
Enjoy the coding :)
Accepted
0
Ravishankar Singh
NA 3.4k 458.3k 13y
Hi Crish,
Could you please share your code so that I can tell where you did mistake?(If you dont mind) :)
0
Crish
NA 3.7k 76.4k 13y
I am also new in ajax.

I try this code which mention here but it is not working .

plz help me.


0
Majid Kamali
NA 315 213.4k 13y
My code is exactly like this but file name is different.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","textFile.txt",false);
xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>

And here's textFile.txt:
<p>Hello user. This is a text from server.</p>

What's the problem?
Thanks
0
Ravishankar Singh
NA 3.4k 458.3k 13y
Hi Majid,
You are getting error because you have not added the url in open method
i.e
Replace xmlhttp.open("GET","ajax_info.txt",false); to xmlhttp.open("GET","http://www.w3schools.com/ajax/ajax_info.txt",false);

See the below example that I am showing you:

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","http://www.w3schools.com/ajax/ajax_info.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

Now you continue your ajax learning,Happy coding :)