Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Ajax Beginner

  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax Beginner

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    	var XMLHttpRequestObject = false;
    	if(window.XMLHttpRequest){
    		XMLHttpRequestObject = new XMLHttpRequest();
    	}
    	else if(window.ActiveXObject){
    		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	function getData(dataSource, divID){
    		if(XMLHttpRequestObject){
    			var obj = document.getElementById(divID);
    			XMLHttpRequestObject.open("GET", dataSource);
    			
    			XMLHttpRequestObject.onreadystatechange = function(){
    				if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
    					obj.innerHTML = XMLHttpRequestObject.responseText;
    				}
    			}
    			XMLHttpRequestObject.send(null);
    		}
    	}
    </script>
    </head>
    <body>
    <h1>Fetching Data Using Ajax</h1>
    <form>
    	<input type="button" value="Display Message" onclick="getData('data.txt', 'targetDiv')" />
    </form>
    <div id="targetDiv"><p>The Fetched Data will go here</p></div>
    </body>
    </html>
    I can't trace the error
    data.txt : Ajax

  • #2
    Regular Coder FWDrew's Avatar
    Join Date
    Apr 2008
    Location
    Missouri
    Posts
    380
    Thanks
    38
    Thanked 45 Times in 43 Posts
    Works for me, I just tested it out on my server without any issue. This may be better suited for the JS section.
    Dev-Tips.com || Employee at the Envato Network || My Portfolio

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, it works.
    So ajax must works on server side? I used .php just now.
    Before : I used .html -> not working

    Thanks palz ^^

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by sysout View Post
    yeah, it works.
    So ajax must works on server side? I used .php just now.
    Before : I used .html -> not working

    Thanks palz ^^
    Sounds like you're testing by double clicking the file (ie: via file:// context instead of http:// context). IE will disable that ActiveX by default (unless its changed since IE6 which wouldn't suprise me). The only thing the server cares about is if the request is to a server processed page. And even then, it functions no differently from a standard page bar syntactically correct data is returned (XML).
    Moving from PHP to JS.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    jQuery
    Code:
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    	$("#clickme").click(function(){
    		$.ajax({
    			url: "page.html",
    			cache: false,
    			success: function(html){		
    				$("#psn").html(html);
    				}
    		});
    	});
    });
    </script>
    <title>Untitled Document</title>
    </head>
    
    <body>
    <a href="#" id="clickme"> click me
    </a>
    <div id="psn"></div>
    </body>
    </html>

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nice help palz ^^

  • #7
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's a very interesting topic. But this field is still new to me. It will be grateful if you give me some more information about it. Thanks in advance.
    __________________
    Data entry-Virtual Assistant


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •