CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    'callback' is undefined

    I set up a simple website using ajax framework. The code of html page is as below:
    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>
        <title>Getting XML with ReadXmlWithGet</title>
    
        <script type="text/javascript" src="ajaxframework.js" />
    
        <script type="text/javascript" >
    
        function callback(xml)
        {
            xmlData = xml.getElementByTagName("data");
            document.getElementById("targetDiv").innerHTML = xmlData(0).firstChild.data;
        }
        </script>
    
    </head>
    <body>
        <h1>Getting XML with ReadXmlWithGet
        </h1>
        <form>
            <input onclick="readXmlWithGet('data.xml', callback)" type="button" value="Display Msg 1" />
        </form>
        <div id="targetDiv" height="100" width="100">The fetched data will appear here.
        </div>
    </body>
    </html>
    When I click button after running from Visual Studio 2005, I get error called "callback is undefined."
    I am new to this. Anybody could tell me why?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: 'callback' is undefined

    Put the function callback above the reference to ajaxframework.js. If that doesn't work, then can you please post the source to that JS file so we can see more.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: 'callback' is undefined

    Quote Originally Posted by PeejAvery
    Put the function callback above the reference to ajaxframework.js. If that doesn't work, then can you please post the source to that JS file so we can see more.
    Thank you for your reply.

    I put the reference under and got blank page. The code of ajaxframework.js is as below:
    Code:
    function readXmlWithGet(urlToCall, functionToCallback)
    {
    	var XMLHttpRequestObj = false;
    	
    	if (window.XMLHttpRequest)
    	{
    		XMLHttpRequestObj = new XMLHttpRequest();
    	}
    	else if (window.ActiveObject)
    	{
    		XMLHttpRequestObj = new ActiveObject('Microsoft.XMLHTTP');
    	}
    	
    	if (XMLHttpRequestObj)
    	{
    		XMLHttpRequestObj.open('GET', urlToCall);
    		
    		XMLHttpRequestObj.onreadystatechange = function()
    		{
    			if (XMLHttpRequestObj.readyState == 4 && XMLHttpRequestObj.status == 200)
    			{
    				functionToCallback(XMLHttpRequestObj.responseXML);
    				delete XMLHttpRequestObj;
    				XMLHttpRequestObj = null;
    			}
    		}
    		
    		XMLHttpRequestObj.send(null);
    	}
    }

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: 'callback' is undefined

    I see it now. <script> must have an end tag not an end slash. This would also explain the blank page when you changed them around. It would recognize the following text as part of the script and not HTML. Change...
    Code:
    <script type="text/javascript" src="ajaxframework.js" />
    To...
    Code:
    <script type="text/javascript" src="ajaxframework.js"></script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: 'callback' is undefined

    Thanks, PeejAvery.

    I have another question, which is how I can debug javascript in Visual Studio 2005. I set breakpoint but IDE says that this breakpoint will not currently be hit.

    Anybody else can tell me why?

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: 'callback' is undefined

    Well, I personally like the Error Console in Firefox. I don't work much with Visual Studio 2005. To debug in that, you need to do a multi-step process.

    1. Open Control Panel.
    2. Open Internet Options.
    3. Select the Advanced tab.
    4. Uncheck both boxes that say "Disable script debugging".
    5. Go to Visual Studio 2005.
    6. Click on the Debug menu.
    7. Select Windows.
    8. Choose Script Explorer. (CTRL-ALT-N)
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: 'callback' is undefined

    Thank you. I forget to uncheck the option in IE.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: 'callback' is undefined

    You're welcome. Good luck with the rest of your project.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured