AJAX request to a text file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1406 Views - Last Post: 08 August 2013 - 01:37 AM

#1 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

AJAX request to a text file

Posted 01 August 2013 - 05:57 AM

I have no idea what I'm doing wrong, looked in the error console and it said "Uncaught SyntaxError: Unexpected token { ".

<html>
<head>

</head>
<body>


<script>

function go() {
var test;
function ajax() {
try {

var request = new XMLHttpRequest();

}
catch  {

try {
var request  = new ActiveXObject("Microsoft.XMLHTTP");
}

catch {
try {
var request = ActiveXObject("Msxml2.XMLHTTP");
}

catch {
try {
alert("Not supported");
}

}
}
}
request.onstatechange = function () {
if(this.readyState == 4 + this.responseText == 200 + this.responseText != null) {
documeent.getElementById('info').innerHTML  = this.responseText;
}

}
test.open("GET", "text.txt", true);
test.send();
}
</script>

<input type="button" value="go" onclick ="go()" />
<div id="info">
Result:
</div>

</body>

</html>


Is This A Good Question/Topic? 0
  • +

Replies To: AJAX request to a text file

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 01 August 2013 - 06:03 AM

count your braces.

indentation would have shown up this problem right from the start.
Was This Post Helpful? 1
  • +
  • -

#3 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: AJAX request to a text file

Posted 01 August 2013 - 07:30 AM

It still isn't working, I have balanced the number of braces. Yet the error persists, also I have another error "Uncaught ReferenceError: go is not defined"
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 01 August 2013 - 07:36 AM

post the updated code.
Was This Post Helpful? 0
  • +
  • -

#5 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: AJAX request to a text file

Posted 01 August 2013 - 02:55 PM

Formatting you code from post #1 differently
should help you see where the missing } might be.

<html>
<head>
</head>
<body>

<script>

function go() {
 var test;
 function ajax() {
  try { var request = new XMLHttpRequest(); }
  catch  { 
   try { var request  = new ActiveXObject("Microsoft.XMLHTTP"); }
   catch {
    try { var request = ActiveXObject("Msxml2.XMLHTTP"); }
    catch {
     try { alert("Not supported"); }
    }
   }
  }
  request.onstatechange = function () {
   if(this.readyState == 4 + this.responseText == 200 + this.responseText != null) {
    documeent.getElementById('info').innerHTML  = this.responseText;
   }
  }
  test.open("GET", "text.txt", true);
  test.send();
 }
[color="#FF0000"]
// NOTE: unbalanced '}' by the time you get to the end of the function
[/color]
</script>

<input type="button" value="go" onclick ="go()" />
<div id="info"> Result: </div>

</body>
</html>



Note 1: You also have a unbalanced try-catch section at the end.

Note 2: If your 'text.txt' is not on the server, it may not work anyway.
I don't think you can use AJAX on a local drive without your own dedicated server.

This post has been edited by JMRKER: 01 August 2013 - 02:59 PM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3475
  • View blog
  • Posts: 11,813
  • Joined: 12-December 12

Re: AJAX request to a text file

Posted 01 August 2013 - 03:46 PM

request.onstatechange = function () {

should be onreadystatechange, you've misspelt document and the and operator is &&, not +.

This post has been edited by andrewsw: 01 August 2013 - 03:46 PM

Was This Post Helpful? 2
  • +
  • -

#7 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: AJAX request to a text file

Posted 02 August 2013 - 02:24 AM

Still the same errors haha, sorry guys :P
<!DOCTYPE html>
<html>
<head>Sending an Ajax request</head>

<body>

<input type="button" value="Click me" onclick="go()" />
<div id="info">
This will be replaced
</div>
<script>
function go() {//open function go//
var test;
function ajax {//open function ajax//

try {//open try 
var request  = new XMLHttpRequest();}//close try 1//
catch {//open catch 1//

try{ //open try 2//

var request = new ActiveXObject("Microsoft.XMLHTTP");//IE 5//
}//close try 2
catch {//open catch 2//

try {//open try 3//
var request = new ActiveXObject("Msxml2.XMLHTTP");//for IE 6+//
}//close try 3//
catch {//open catch 3//
alert("Not supported");
}//close catch 3//
} //close catch 2//
}//close catch 1//
}//close function ajax

request.onreadystatechange = function() { //open onreadystatechange function//


if(this.readyState==4 && this.responseText==200 && this.responseText !=null) {//open technical invalidators
document.getElementById('info').innerHTML = this.responseText; 

}//close technical invalidators//
}//close onreadystatechange function//

test.open("GET", "text.txt", true);
test.send();
}//close function go//



</script>


</body>

</html>


Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 02 August 2013 - 02:35 AM

there shouldn’t be an error about braces, there should be an "object is null or not an object" error.

EDIT: carefully read line #14, and check what’s wrong there.

since you mentioned errors, which errors else that you didn’t tell us?

additionally, this.responseText==200 && this.responseText !=null is only true if your text file only ever contains the string '200' (and nothing else, even a line break)

This post has been edited by Dormilich: 02 August 2013 - 02:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: AJAX request to a text file

Posted 02 August 2013 - 03:52 AM

You really need to listen and INDENT YOUR CODE, so you can get rid of all those silly comments. Also, get an editor that supports parenthesis matching.

Here's what your code should look like, properly indented:

<!DOCTYPE html>
<html>
	<head>Sending an Ajax request</head>

	<body>
		<input type="button" value="Click me" onclick="go()" />
		<div id="info">
			This will be replaced
		</div>
		<script>
			function go() {
				var test;
				function ajax {

					try {
						var request  = new XMLHttpRequest();
					} catch {
						try{
							var request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch {
							try {
								var request = new ActiveXObject("Msxml2.XMLHTTP");
							} catch {
								alert("Not supported");
							}
						}
					}
				}

				request.onreadystatechange = function() {
					if(this.readyState==4 && this.responseText==200 && this.responseText !=null) {
						document.getElementById('info').innerHTML = this.responseText; 
					}
				}
				test.open("GET", "text.txt", true);
				test.send();
			}
		</script>
	</body>
</html>




I'd put the script in the head tag.

And do you REALLY need to support IE5? Consider using jQuery instead and get the majority of this code for free.

<!DOCTYPE html>
<html>
	<head>
		<title>Sending an Ajax request</title>
		<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

		<script>
			function go() {
				$.get('test.txt', function(data) {
					$('#myData').html('<p>' + data + '</p>');
				})
			}

			$(function() {
				$('#myButton').bind('click', go);
			});
		</script>
	</head>
	<body>
		<div id="myContent">
			<input type="button" value="Click me" id="myButton" />
			<span id="myData"></span>
		</div>
	</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 02 August 2013 - 04:12 AM

could be further condensed into
function go() {
	$('<p/>').load('test.txt').appendTo($('#myData'));
}



Was This Post Helpful? 1
  • +
  • -

#11 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: AJAX request to a text file

Posted 02 August 2013 - 06:49 AM

Alright, I have indented the code. Fixed up the spellings, changed line #14 and managed to get it working using jquery. However, I want to get it working here. So, I have one more error: Uncaught TypeError: Cannot set property 'onreadystatechange' of undefined.


<!DOCTYPE html>

<html>

    <head>Sending an Ajax request</head>



    <body>

        <input type="button" value="Click me" onclick="go()" />

        <div id="info">

            This will be replaced

        </div>

        <script>

            function go() {

                var test;

                function ajax() {

 

                    try {

                        var request  = new XMLHttpRequest();

                    } catch {

                        try{

                            var request = new ActiveXObject("Microsoft.XMLHTTP");

                        } catch {

                            try {

                                var request = new ActiveXObject("Msxml2.XMLHTTP");

                            } catch {

                                alert("Not supported");

                            }

                        }

                    }

                }

 

                request.onreadystatechange = function() {

                    if(this.readyState==4 ) {

                        document.getElementById('info').innerHTML = this.responseText;

                    }

                }

                test.open("GET", "text.txt", true);

                test.send();

            }

        </script>

    </body>

</html>




Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 02 August 2013 - 07:53 AM

Quote

So, I have one more error

that’s the one I predicted in post #8.

anyhow, where in go() (and not in ajax()) do you set the variable request?
Was This Post Helpful? 0
  • +
  • -

#13 poliosynthesis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-June 13

Re: AJAX request to a text file

Posted 03 August 2013 - 10:00 PM

I don't think I do, do I have to put the line in:
request = new ajaxRequest();

This post has been edited by poliosynthesis: 03 August 2013 - 10:01 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: AJAX request to a text file

Posted 04 August 2013 - 11:38 AM

that depends on ajaxrequest().
Was This Post Helpful? 0
  • +
  • -

#15 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: AJAX request to a text file

Posted 04 August 2013 - 12:53 PM

View PostDormilich, on 02 August 2013 - 11:12 AM, said:

could be further condensed into
function go() {
	$('<p/>').load('test.txt').appendTo($('#myData'));
}




IMO this is the easiest way to accomplish this.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3