Jump to content

understanding ajax


newrehmi

Recommended Posts

hello all,Finally i have been able to have a grasp in understanding xml structure, and accessing it through DOM. Then I continued my studies into Ajax, which i too has been able to have a bit grasp in understanding it. However, there's a still a few question puzzling in my mind about ajax.1. True that javascript were used to access xml through DOM, but, is there is any connection between method: createElement or setAttribute or anything, in term of altering XML so that the xml work as what I understand it should works in ajax (as a temporary storage).2. Or do we need to pass a parameter in XMLHttpRequest object through JS method 'open' using GET? so that, the PHP or ASP file can retrieve the value and make an fwrite upon the .XML file and make an insert query into database. Is this understanding correct?3. If we do need to use PHP to make a write upon the XML files, then my first assumption about ajax is wrong then, since it should only based on the language of xml and javascript (and html).I haven't tried it much but just by understand the concept might drive me further in learning it. Thanks alot.

Link to comment
Share on other sites

If you use AJAX to load an XML document into the browsing context, you can manipulate the XML DOM using DOM methods. When you're done, serialize the DOM back into a string and POST it to your server using AJAX. All your server needs to do is save the string as a file.

Link to comment
Share on other sites

hello,i've finally been able to make a change to some text using fwrite. But i detected some bug that i didn't understand. First before anything, this is my script:

function send()	{	if (XMLHttpRequest)		{		var http = new XMLHttpRequest();		}	else 		{		var http = new ActiveXObject('Microsoft.XMLHTTP');		}	var thetext = document.getElementById('ajax_texttoinsert').value;	var date = new Date();	thetext += ', <i>written on '+date.getHours()+':'+date.getMinutes()+'</i>';	http.open('GET','ajax_server.php?thetext='+thetext,true);	http.send(null);	http.onreadystatechange = function()		{		if (http.readyState == 4 && http.status == 200)			{			get()			}		else if (http.status != 200)			{			alert('it is not working!!!')			}		}	}	function get()	{	if (XMLHttpRequest)		{		var http2 = new XMLHttpRequest();		}	else 		{		var http2 = new ActiveXObject('Microsoft.XMLHTTP');		}	http2.open('GET','the.txt',true);	http2.send(null);	http2.onreadystatechange = function()		{		if(http2.readyState == 4 && http2.status == 200)			{			document.getElementById('ajax_thetextplace').innerHTML = http2.responseText			}		}	}

<body onload='get()'>		<div id='ajax_thetextplace'></div>		<br>Please insert any text below:<br>		<input type='text' id='ajax_texttoinsert' /><input id='ajax_submit' type='submit' onclick='send()' /></body>

the bug that am talking about is, it's true that the script is running perfect all the time, but sometimes, it's not working. It's like not calling function get() after I submit a new value, but when I reload the page, it run.Is there's something that I still missing like timeout or idling time or anything? or server problem?Thanks alot by the way.

Link to comment
Share on other sites

Is that your complete script? If it is, then of course you need a tool that calls get() . setInterval is the most likely way to do it. Something like this should work.setInterval(get, 3000);You'll probably want to put that statement in an onload handler. Something like this in the global space of your script:

var myInterval;window.onload = function () {   myInterval = setInterval(get, 3000);}

In fact, with that done, you can even remove the onLoad attribute from your <body> tag.

Link to comment
Share on other sites

Is that your complete script? If it is, then of course you need a tool that calls get() . setInterval is the most likely way to do it. Something like this should work.
yeah that's my complete script, the function is called in this part (bold), please re-check:
function send() { if (XMLHttpRequest) { var http = new XMLHttpRequest(); } else { var http = new ActiveXObject('Microsoft.XMLHTTP'); } var thetext = document.getElementById('ajax_texttoinsert').value; var date = new Date(); thetext += ', <i>written on '+date.getHours()+':'+date.getMinutes()+'</i>'; http.open('GET','ajax_server.php?thetext='+thetext,true); http.send(null); http.onreadystatechange = function() { if (http.readyState == 4 && http.status == 200) { get() } else if (http.status != 200) { alert('it is not working!!!') } } }
once I submitted a new value, it will call function get(). It worked all the times, but sometimes it fails. I wonder why.Thanks for your times :)
Link to comment
Share on other sites

Oh, I see. The first thing that strikes me is you're using the GET protocol instead of the POST protocol. They are not the same. A basic part of the GET request is that if the request data is the same as a previous request, if the result of the previous request is cached, the old result will be used. This is why images and stuff reload "immediately" if they've been loaded before. What you're looking at is the cached version.Here's your GET request:ajax_server.php?thetext='+thetextSince you are testing, I'll bet that sometimes the value of thetext is the same as another time you sent the request. So nothing new will show up. Or something old might show up.Plain and simple, try switching the request mode to POST. See if it makes a difference.

Link to comment
Share on other sites

Oh, I see. The first thing that strikes me is you're using the GET protocol instead of the POST protocol. They are not the same. A basic part of the GET request is that if the request data is the same as a previous request, if the result of the previous request is cached, the old result will be used. This is why images and stuff reload "immediately" if they've been loaded before. What you're looking at is the cached version.Here's your GET request:ajax_server.php?thetext='+thetextSince you are testing, I'll bet that sometimes the value of thetext is the same as another time you sent the request. So nothing new will show up. Or something old might show up.Plain and simple, try switching the request mode to POST. See if it makes a difference.
Thank you sooo much deirde's father. Now it relief me >,< for knowing what is the problem now. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...