CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: getdata()

  1. #1
    Join Date
    Aug 2005
    Posts
    98

    getdata()

    Hello firends

    I'm learning ajax ,


    From AJAX for dummies:



    Checking to make sure you have
    a valid XMLHttpRequest object
    Now that you’ve got the needed
    XMLHttpRequest object stored in the variable


    XMLHttpRequestObject
    , how do you actually fetch the text the application
    wants when the user clicks the button? All that takes place in the


    getData
    function in the <script> element, as shown here:

    Code:
    
    
    <script language = “javascript”> . . . function getData(dataSource, divID) { . . . } </script>

    I need to know more about this function.

    May someone give me complete example about this function?getdata()?(Its syantax,Tutorial for that...)

    GOOD LUCCK!

  2. #2
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278

    Re: getdata()

    well I don't have the AJAX for dummies book, but I'll give it a shot.

    the getData() javascript method is just an arbitrary name the sample in the book is using. it could be called anything. by looking at the received arguments (datasource,divID) i'd say that datasource is the name of the 'listening' servlet URL (I'm using Java for my example) and divID is the ID of the DIV tag whose contents you are going to replace with the resulting XML response.

    your method will construct a URL on the fly, retrieving form parameters the user has entered, or clicked on.
    eg:
    Code:
    url= "autocomplete?action=complete&id=" + escape(completeField.value);
    var req = initRequest(url);
            req.onreadystatechange = function() {
    			
                if (req.readyState == 4) {
                    if (req.status == 200) {
    			
                        parseMessages(req.responseXML);
                    } else if (req.status == 204){
    				
                        clearTable();
                    }
                }
            };
            req.open("GET", url, true);
            req.send(null);
    on the other end (java sample) you have a servlet written to receive a 'GET' method, build a query based on the passed in parameters, then construct an XML document from the returned database results. send the XML object back out, add the servlet to your WEB.XML configuration file, and it's ready to roll.

    parseMessages() will receive the passed parameter (the XML document returned from the server) and simply construct a DIV tag heirarchy (or whatever you program it to do) that will then replace the innerHTML of the received divID argument. then you can simply allow CSS to position and format the objects rewritten to the document.
    Last edited by dmeikle; September 10th, 2006 at 04:32 PM.
    David Meikle
    Quantum Unit Solutions, LLC
    www.quantumunit.com

  3. #3
    Join Date
    Aug 2005
    Posts
    98

    Re: getdata()

    Hello firend

    thanks for answer

    I'm reading another book about AJAX

    From AJAX in action page 47

    When the user logs in or otherwise initializes a session, several server-side
    objects are created, representing, say, the shopping basket and the customer credentials
    if this is an e-commerce site. At the same time, the home page is dished
    up to the browser, in a stream of HTML markup that mixes together standard
    boilerplate presentation and user-specific data and content such as a list of
    recently viewed items.
    Every time the user interacts with the site, another document is sent to the
    browser, containing the same mixture of boilerplate and data. The browser dutifully
    throws the old document away and displays the new one, because it is dumb
    and doesn’t know what else to do.



    "Every time the user interacts with the site, another document is sent to the
    browser, containing the same mixture of boilerplate and data. The browser dutifully"

    May someone give me example about this?




    when We use GET method I know we pass data to URL ,Then Why we need to send boilerplate?why do we need to send HTML?

    What is running in AJAX "asynchronous"?Which part are runing "asynchronous"?May someone make it clear by an example?

  4. #4
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278

    Re: getdata()

    the server will return an XML file. Typically, you (the developer of the server side script) will DEFINE how the XML is constructed. This makes it a boilerplate. The receiving code will ALWAYS receive the exact same structured XML document. although the data contained within the document might change, the XML structure will always be the same (boilerplate template).

    so by this example, I "could" say the boilerplate is (in this instance) simply the STRUCTURE of the XML document.
    eg:
    Code:
    <cars>
      <car>red car</car>
      <car>blue car</car>
    </cars>
    and perhaps on the next query:
    Code:
    <cars>
      <car>green car</car>
      <car>white car</car>
      <car>orange car</car>
      <car>blue car</car>
    </cars>
    notice how the structure of the xml document (cars->car) stayed the same (boilerplate template) but the data changed. that's what they mean.
    David Meikle
    Quantum Unit Solutions, LLC
    www.quantumunit.com

  5. #5
    Join Date
    Aug 2005
    Posts
    98

    Re: getdata()

    Hello firend ,thanks for answer,

    I have anoher question,

    From:
    http://www.w3schools.com/ajax/ajax_database.asp



    The AJAX JavaScript
    This is the JavaScript code stored in the file "selectcustomer.js":

    var xmlHttp

    function showCustomer(str)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    var url="getcustomer.asp"
    url=url+"?q="+str
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
    }




    May you explain about this line?

    url=url+"&sid="+Math.random()

    Why does it create sid?
    If we use PHP,Must I do that also?


    Thanks in advance.

  6. #6
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278

    Re: getdata()

    appending the &sid='+Math.Random() will place a random number at the end of the 'get' request. that makes it seem like a 'unique' url request, and will ensure you always receive a new response from the server (refreshed) rather than your browser displaying a cached response.
    David Meikle
    Quantum Unit Solutions, LLC
    www.quantumunit.com

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