Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926

    Post AJAX - generic function plus full sample code.


    Yet another AJAX sample code, only this time bundled somewhat
    better, at least in my opinion.

    The below is consisted of three files:
    1. The AJAX "engine" which is pure JavaScript code and should
      be saved as .js file. (in the sample code: "Shadow_AJAX.js")
    2. Some sort of server side code that AJAX will call. in the sample
      code here it's AJAX_ReadFile.asp
    3. The page that use the AJAX, in the sample code: Test_AJAX.html


    Also, attached all files plus text file for your convenience - just
    unzip the files to ASP web folder and execute Test_AJAX.html
    from there to see it working.

    --Shadow_AJAX.js
    Code:
    /*
    	this function creates and returns XMLHTTP component
    	if such can be created by the browser.
    */
    function GetXmlHTTP() {
    	//first check for IE:
    	if (window.ActiveXObject) {
    		var objXML = 0;
    		try {
    			objXML = new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch(ex) {
    			try {
    				objXML = new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch(ex) {
    				alert("AJAX: your browser does not support proper XMLHTTP");
    			}
    		}
    		return objXML;
    	}
    	
    	//maybe Mozilla?
    	if (window.XMLHttpRequest)
    		return new XMLHttpRequest();
    	
    	//unknown browser..
    	alert("AJAX: unknown browser.");
    	return 0;
    } //end function GetXmlHTTP
    
    /*
    	this function send request to the given URL via
    	GET method.
    */
    function SendAjaxRequest(strURL, strCallbackFunction) {
    	//get xmlhttp component:
    	var objXML = GetXmlHTTP();
    	
    	//got anything?
    	if (!objXML)
    		return false;
    	
    	//attach local callback function:
    	objXML.onreadystatechange = function()
    	{
    		AjaxPageLoad(objXML, strCallbackFunction);
    	}
    	
    	//send request:
    	objXML.open("GET", strURL, true);
    	objXML.send(null);
    } //end function SendAjaxRequest
    
    /*
    	this function is called after we got response
    	from the server.
    */
    function AjaxPageLoad(objXML, strCallbackFunction) {
    	//ready?
    	if (objXML.readyState != 4)
    		return false;
    
    	//get status:
    	var status=objXML.status;
    	
    	//maybe not successful?
    	if (status != 200) {
    		alert("AJAX: server status "+status);
    		return false;
    	}
    	
    	//get response text:
    	var strResponse = objXML.responseText;
    	
    	//call function
    	eval(strCallbackFunction+"(\""+ReplaceLines(strResponse.replace(/"/g, "\\\""))+"\");");
    	return true;
    } //end function AjaxPageLoad
    
    function GlobalReplace(strOriginal, strToReplace, strReplaceWith) {
    	var result=strOriginal;
    	if ((strOriginal.length == 0)||(strToReplace.length == 0)||(strToReplace == strReplaceWith))
    		return result;
    	while (result.indexOf(strToReplace) >= 0)
    		result = result.replace(strToReplace, strReplaceWith);
    	return result;
    }
    
    function ReplaceLines(s) {
    	var result = s;
    	result = GlobalReplace(result, "\n\r", "\n");
    	result = GlobalReplace(result, "\r\n", "\n");
    	result = GlobalReplace(result, "\n", "<br />");
    	return result;
    }
    --AJAX_ReadLines.asp
    Code:
    <% Option Explicit %>
    <%
    	Const FILE_NAME = "test_ajax.txt"
    	Dim objFSO, objFile, strFilePath
    	
    	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    	strFilePath =  Server.MapPath(FILE_NAME) 
    	
    	If Not(objFSO.FileExists(strFilePath)) Then
    		Response.Write("ERROR: file does not exist")
    	Else  
    		Set objFile = objFSO.OpenTextFile(strFilePath)
    		If objFile.AtEndOfStream Then
    			Response.Write("ERROR: file is empty")
    		Else  
    			Response.Write(objFile.ReadAll)
    		End If
    		objFile.Close
    		Set objFile = Nothing
    	End If
    	
    	Set objFSO = Nothing
    %>
    --Test_AJAX.html
    Code:
    <html>
    <head>
    <script type="text/javascript" src="Shadow_AJAX.js"></script>
    <script type="text/javascript">
    window.onload = WindowLoad;
    function WindowLoad(event) {
    	SendRequest();
    	window.setInterval("SendRequest()", 5000);
    }
    
    function SendRequest() {
    	SendAjaxRequest(BustCache( "http://localhost/General/AJAX_ReadFile.asp" ), "AnalyzeResponse")
    }
    
    function AnalyzeResponse(strResponse) {
    	document.getElementById( "FileContents" ).innerHTML = strResponse;
    }
    
    function BustCache(strURL) {
    	var strRandom = "nnn=" + parseInt((Math.random()*999999)+1);
    	return strURL + ((strURL.indexOf("?") == -1)?"?":"&") + strRandom;
    
    }
    </script>
    </head>
    <body>
    The contents of the text file: <div id="FileContents"></div>
    </body>
    </html>
    Remarks:
    • This version of AJAX will replace new line characters with
      the HTML "<br />" new line tag before sending the response
      back to the client function.
    • This AJAX is using "cache buster" to avoid caching issues.
    • This example will show the contents of text file and "refresh"
      those contents every 5 seconds.


    Happy Programming!
    Attached Files
    Last edited by Shadow Wizard; December 3rd, 2006 at 02:35 PM.
  2. #2
  3. Shemzilla
    ASP Scholar (3000 - 3499 posts)

    Join Date
    Aug 2005
    Location
    Table View, Cape Town, S.A
    Posts
    3,397
    Rep Power
    561
    I unzipped and ran Test_AJAX.html

    all i get is an alert error 404?
    Everyone has a photographic memory. Some don't have film.
    I am a nobody, nobody is perfect, therefore I am perfect.

  4. #3
  5. Shemzilla
    ASP Scholar (3000 - 3499 posts)

    Join Date
    Aug 2005
    Location
    Table View, Cape Town, S.A
    Posts
    3,397
    Rep Power
    561
    got it working
  6. #4
  7. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    Originally Posted by shem
    got it working
    well done!
  8. #5
  9. Shemzilla
    ASP Scholar (3000 - 3499 posts)

    Join Date
    Aug 2005
    Location
    Table View, Cape Town, S.A
    Posts
    3,397
    Rep Power
    561
    would it be difficult to change this to work with a form to
    update a database?

    Shem
  10. #6
  11. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    yes you'll have to change the ASP and HTML files.
    it's not going to be simple - if you need help on that
    start new thread and I'll do my best.
  12. #7
  13. Shemzilla
    ASP Scholar (3000 - 3499 posts)

    Join Date
    Aug 2005
    Location
    Table View, Cape Town, S.A
    Posts
    3,397
    Rep Power
    561
    which forum would I post this in?

    js forum?
  14. #8
  15. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    ASP forum, as the "core" is ASP.

    Comments on this post

    • shem agrees : thanks:D
  16. #9
  17. No Profile Picture
    ssj
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Feb 2007
    Posts
    30
    Rep Power
    13
    Got it working nicely
  18. #10
  19. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2007
    Location
    Atlanta
    Posts
    11
    Rep Power
    0
    I don't want to appear to be the village idiot but I tried this in IE 7 and FF 2 and got nothing but:

    The contents of the text file:

    and BTW, why can't I upload an avatar on this board?
  20. #11
  21. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    how exactly did you try it?
    do you have IIS installed?

    regarding avatar, you have to be member for 30 days and have 30
    posts before becoming Contributing Member that can have avatar
    and other cool stuff.
  22. #12
  23. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2007
    Location
    Atlanta
    Posts
    11
    Rep Power
    0
    I published everything in the .zip on my server (with dozens of other ASP site) in the root of the web and opened the test_AJAX.html

    It's a Windows 2003 server with IIS 6

    I am also seeing a permission denied error on line 52 when I pull up this page in IE. No error in FF. Do I need to set any special permissions on any of the files?
  24. #13
  25. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2007
    Location
    Atlanta
    Posts
    11
    Rep Power
    0
    I think I found the problem. The function SendRequest in Test_AJAX.html contained a path to localhost. I changed it and the text is coming up.

    Now I have to fit it in to my application.

    Thanks, I'll let you know how it goes.

  26. #14
  27. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2007
    Location
    Atlanta
    Posts
    11
    Rep Power
    0
    I'm also not seeing the 5 second refresh happen. Everything writes out almost immediately then nothing.

    However, if I let the browser sit for a while it comes back with the error: AJAX: server status 12029
  28. #15
  29. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    thanks for sharing the solution - FYI you can dynamically grab the server
    name by using Request.ServerVariables("SERVER_NAME") in ASP, I haven't
    done that since my example is generic and is not language dependent.

    regarding the 5 second refresh there is no such thing in my code. post
    in your other thread about it with full details and we'll see.
Page 1 of 2 12 Last
  • Jump to page:

Similar Threads

  1. help me please
    By vb123 in forum Visual Basic Programming
    Replies: 1
    Last Post: September 3rd, 2006, 01:27 PM
  2. Login procedure with .NET 2.0 & SQL Server 2000
    By mickyjtwin in forum .NET Development
    Replies: 2
    Last Post: December 29th, 2005, 06:31 PM
  3. calling a function in html code!
    By mansoorafzal in forum .NET Development
    Replies: 8
    Last Post: June 22nd, 2005, 10:54 AM
  4. problem with function
    By wackyflik in forum ASP Development
    Replies: 14
    Last Post: June 9th, 2005, 05:52 AM
  5. function writing problem
    By vb123 in forum Visual Basic Programming
    Replies: 13
    Last Post: November 13th, 2003, 05:56 AM

IMN logo majestic logo threadwatch logo seochat tools logo