Can I use this Ajax script to communicate and exchange data between client and server?
Posted
by lala
on Stack Overflow
See other posts from Stack Overflow
or by lala
Published on 2010-06-04T09:53:58Z
Indexed on
2010/06/07
11:52 UTC
Read the original article
Hit count: 203
AJAX
This block of code is for client.html (it is located in this www.client.com/client.html) - client side.
The I have the code below that goes something like this:
ajaxRequest.open("GET", "http://www.server.com/ajax.php", true);
This is how I call the file ajax.php located in the server. Unfortunately I have no luck at all. It cannot connect to the server I'm calling. BTW, the ips /test site I've been using are all no restrictions, and is accessible to all.
However, I tried to simulate by putting both client.html and ajax.php in same site and it works well.
So my question is does this script works only if you are in same site? or does it work also in client-server scenario? What else do I have to do in order to make this work?
//client.html
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "http://www.server.com/ajax.php", true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Name: <input type='text' onChange="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>
// ajax.php
<?php
echo date("H:i:s");
?>
© Stack Overflow or respective owner