Jump to content

Javascript doesn't want to run AJAX!

- - - - -

  • Please log in to reply
8 replies to this topic

#1
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
OK I decided this is more of a java script problem than a AJAX as firebug reports no errors or communication attempts.

Ok so I have my HTML file's body (the script file is loading according to firebug).
	

<BODY onload='load_list(1)'>

		<CENTER>

			<div id='list_container'>

				MySQL List Displayed using AJAX

					<div id='list'>

						Page 1

					</div>

				Add an Item

			</div>

		</CENTER>

	</BODY>

When it loads its meant to run the "load_list" function passing the variable "1".
Next I have my script.js file (firebug shows this being loaded)
 

var request;

function load_list(page)	{

	if (window.XMLHttpRequest())	{

		request = new XMLHttpRequest();

	} else	{

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

	}

	request.xmlreadystatechange=function()	{

		if (request.readyState==4 && request.status==200)	{

			document.getElementByID("list").innerHTML=request.responseText;

		}

	}

	request.open("GET","getList.php" + page,true);

	request.send;

}

Finally I have my PHP file getList.php, it seems to be working fine but I'll post the HTML code it generates anyway:

<table width='550px'>

<tr> 

<td> 1 </td> 

<td> Bob </td> 

<td> Bob </td> 

<td> 2011-05-14 11:14:50 </td> 

</tr>

<tr> 

<td> 2 </td> 

<td> Jem </td> 

<td> Bob </td> 

<td> 2011-05-14 11:14:50 </td> 

</tr>

</table>

Please DO NOT FLAME ME!
I understand some people will just swim through my text only noticing that my PHP code is HTML output. As the PHP code is working fine I decided not to post it!
Well basically that does absolutely nothing, if I put in a alert message in the load_list function it works through.

After a while I downloaded firebug, here is the output (css.css is my CSS file).

Quote

[13:56:10.815] GET http://localhost/tes..._list/script.js [HTTP/1.1 304 Not Modified 4ms]
[13:56:10.798] GET http://localhost/tes...ax_list/css.css [HTTP/1.1 304 Not Modified 3ms]
[13:56:10.723] GET http://localhost/test/ajax_list/ [HTTP/1.1 200 OK 5ms]

Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
One observation - you appear to be using "getList.php" + page rather than "getList.php?p=" + page or however you had set it up.

It could be making a valid request, although returning no content.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
ehm, I think status 304 not modified, just means that the file you're tryign to get has not changed compared to the previous download, telling the browser to use its cache.
It's actually no bad status number.

You got 2 options.
  • Remember the last reponse, (store in global variable) and use this if the reponse status == 304.
  • add a random number to the url.
    
    request.open("GET","getList.php" + page [B]+ "&nocache=" + Math.random()[/B] ,true);
    
    
    I think this should avoid getting a 304 telling the browser to use its cache, as it's a totally "new" request.


#4
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Status 304 isn't my problem!
It loaded fine with 200's earlier... (I know what it is seeing as it gives a nice description!)

The problem is Javascript isn't making any attempt to run the AJAX code...
Just to check it was linking I opened the source, and added some alerts that functioned fine!
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#5
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Ok, ads are causing codecall to not load the edit page :(.

You obviously misread my code the 304 error is loading my CSS and Javascript files NOT the page AJAX is meant (getList.php) to load, infact no attempt whatsoever is recorded by firebug....
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I've ran your code through JSlint, and there appears to be an error within the function to check for status, I cannot see any obvious error however you may wish to compare your code with one that works.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Ok I'll try that now...
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#8
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Ok, i forgot a pair of braces for the send function.
request.send();
JSlint is telling me to change all my if's to === instead of ==?? Doesn't sound right and didn't fix it, It hasn't really helped me...

here is its output:

Quote

Error:

Problem at line 9 character 31: Expected '===' and instead saw '=='.

if (request.readyState==4 && request.status==200) {

Problem at line 9 character 52: Expected '===' and instead saw '=='.

if (request.readyState==4 && request.status==200) {

Problem at line 12 character 6: Expected ';' and instead saw 'request'.

}

Implied global: window 3, XMLHttpRequest 4, ActiveXObject 6, document 10
...
Now i see the firebug request, but it still doesn't update the page, i'll go back to the tutorial and see if I can get some code, I think one of my blocks is meant to be a function...
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#9
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
All fixed.
BTW JSlint appears to be absolutely useless...
It gives me tons of errors even for a fine script (like using three equal signs instead of two, which results in the script not working) and doesn't even notice the real errors.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users