Results 1 to 13 of 13
  1.    #1  
    I am just getting into development and have taken the Javascript and HTML tutorials at W3Schools. I am trying learn how to create a successful Ajax request and then use it in someway. I have figured out how to write an Ajax request but I don't know how to handle the response in a way that I may use it such as caching a particular part of the response so I may use it in later requests. Here is my request: I have filled in a few parts of the URLs with '####' and take the '>' as tab in.
    signinTap: function(inSender, event) {
    >var xmlHttp = new XMLHttpRequest();
    >XmlHttp.open("GET","http://api.###.com/###/xml/session.login?apiKey=##########=" + mainusernameField + "&password=" + main.passwordField + "speed=250",true);
    >XmlHttp.send();
    >XmlDoc = XmlHttp.ResponseXML;
    then a sample response would look like this
    <###SessionId>6E46DACB181C270390F5865D6DB5E5CA</###SessionId>
    <maxInactiveInterval>60</maxInactiveInterval>
    <###Version>2.00.0729</###Version>
    Now what I need to know is what I am doing wrong here, how do I give a variable the value of the SessionID in the response, and how I can use this in the next scene.

    Any and all replies are greatly appreciated.
    Regards,
    Rennat
    Last edited by Rennat; 06/19/2011 at 08:42 PM.
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  2. #2  
    Code:
    var request = new Ajax.Request("http://yoururlhere.com", {
    		method : "get",
    		onSuccess: this.getSuccess.bind(this),
    		onFailure: function(response){
    			Mojo.Log.error("Get request failed");
    		}.bind(this)
    	});
    then your success handler:
    Code:
    MainAssistant.prototype.getSuccess = function(response){
    	var json = response.responseJSON;
    	//now parse/handle your response data as you like
    }
  3.    #3  
    Is it possible to use the Ares ajax request service but use a URL that includes the value of a text plane? What I mean is to use a url like "www.google.com/blah/blah/" + main.userFeild???
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  4. #4  
    Quote Originally Posted by Rennat View Post
    Is it possible to use the Ares ajax request service but use a URL that includes the value of a text plane? What I mean is to use a url like "www.google.com/blah/blah/" + main.userFeild???
    Absolutely. Just build the URL string you need, including any path and parameters and set it as the URL of the Ajax.Request. I build all of the URLs in my forum app dynamically. Note the example json param has nothing to do with your example, that is specific to the URL I am calling.

    Code:
    var url = "http://www.mywebsite.com/pathToResource?type=json&threadid=" + this.threadid;
    var request = new Ajax.Request(url, {
    			method: 'get',
    .... etc
    Edit - I just realized you are asking about the ARES Ajax request... I have never used it, so I can't say for sure, but this is how you do it rolling your own Ajax request.
  5.    #5  
    Quote Originally Posted by Grabber5.0 View Post
    Absolutely. Just build the URL string you need, including any path and parameters and set it as the URL of the Ajax.Request. I build all of the URLs in my forum app dynamically. Note the example json param has nothing to do with your example, that is specific to the URL I am calling.

    Code:
    var url = "http://www.mywebsite.com/pathToResource?type=json&threadid=" + this.threadid;
    var request = new Ajax.Request(url, {
    			method: 'get',
    .... etc
    Edit - I just realized you are asking about the ARES Ajax request... I have never used it, so I can't say for sure, but this is how you do it rolling your own Ajax request.
    Thanks, this is how I am having to do right now because the Ares plugin service doesn't allow you to add any variables to the url. Just a straight up url text.

    Could you help me learn how to handle the response in a way that I can cache the response so I can use it later in other requests? Also I need to know how to extract the SessionID out of the sample response I put in the first post. I really do not know how to read the response in anyway.

    Thanks alot for the help guys! (sorry but as you can tell, I am REALLY green these ways of the force )
    -Rennat
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  6. #6  
    I have a post on here somewhere showing how to parse the xml. I will see if I can find it, or the original example I used. Basically, you just have to navigate the DOM tree to get the values you want (or figure out how to use xpath )
  7.    #7  
    Quote Originally Posted by Grabber5.0 View Post
    I have a post on here somewhere showing how to parse the xml. I will see if I can find it, or the original example I used. Basically, you just have to navigate the DOM tree to get the values you want (or figure out how to use xpath )
    Ya, it looks like xpath is the way to go. The big problem is that everything is slightly different between WebOS development and web browser development you learn at W3Schools.com. I just wish that someone would go out and write a web based standards tutorial that is all based around development for WebOS.
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  8. #8  
    I can't find the post I was looking for, so I'm throwing together a quick example. Hopefully I won't forget anything. I actually used the XmlHttpRequest in my code, but it appears Ajax.Request returns the same thing. If the server sets the right content type on the response, you can skip the first step of parsing the xml, because Ajax will parse it for you and it will be in responseXML. I didn't use xpath, because I am not very familiar with it, I just walked the tree.

    Code:
    // Convert the string to an XML object
      var responseDoc = (new DOMParser()).parseFromString(req.responseText, "text/xml");
      var respBody = responseDoc.getElementsByTagName("impactwebds")[0];
      if (respBody != null)
      {
         ...
        candidates = locationNode[0].getElementsByTagName("candidateAddresses")[0].getElementsByTagName("candidateAddress");
        var candNum = 0;
        while (candidates != null && candNum < candidates.length) 
        {
          thisbanner = " Candidate " + (candNum+1) + ": ";
          longitude = candidates[candNum].getElementsByTagName("geocodeCenterX")[0].firstChild.nodeValue;
          latitude = candidates[candNum].getElementsByTagName("geocodeCenterY")[0].firstChild.nodeValue;
          thisbanner += "(" + longitude + ", " + latitude + ")";
          //othis.controller.get('status').update(geocoderesult);
          Mojo.Controller.getAppController().showBanner({messageText: thisbanner}, "launchArguments", "myCategory");
    
          candNum++;
        }
     
      }
    This is a snippet of the xml document being parsed:
    Code:
    <impactwebds>
      <locationNode>
       <candidateAddresses>
         <candidateAddress>
           <geocodeCenterX>-98.1</geocodeCenterX>
           <geocodeCenterY>38.1</geocodeCenterY>
         </candidateAddress>
       </candidateAddresses>
      </locationNode>
     </impactwebds>
    Not knowing the structure of the xml document you are working with, I can only point you in the general direction. I can't remember if getElementsByTagName will only search the children of the current node or if it will continue to drill down until it finds a match. The [0] notation is what I used when I knew there was only one of something and didn't want to bother with a loop. It might not even be the best/most efficient way to do it, but it works, and I don't feel like rewriting it until something breaks.
  9.    #9  
    Thanks, this helps ALOT! The response I am getting form the server is exact what you see in the first post. The purpose for it is to use what is in the SessionID tags in later requests so it knows who is asking, kind of like login on an account I guess.
    Thanks alot for the help (seriously),
    -Rennat
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  10. #10  
    You're welcome -- glad I could help! Regarding the xml.. is there a tag surrounding all of what you put in the second block with the xml in it? I took that to be just a snippit of the entire response. There is no xml header or root tag in that xml, so it would not be parseable as a document, as there has to be a single root tag (one tag that contains all of the rest within it).

    I'd recommend reading this article. It was the one I first read when learning how to write "ajax" applications several years ago, and I found it immensely helpful in explaining the whole process. Ajax for Java developers: Build dynamic Java applications
  11.    #11  
    Yes, that is the entire response. It doesn't have any headers that tell you what type of file it is. There are tags surrounding the information I need to extract. Thanks for the article, I haven't read it yet but I have skimmed through it and it looks like its chocked full of info. I'll let you know of my findings in it
    "Life is Hard... it's harder if your stupid"
    - John Wayne
  12. #12  
    I am having similar problems trying to read data from a database using using the web service widget with Ares IDE. Any ideas please?
    Last edited by Louis84; 06/23/2011 at 04:34 AM.
  13.    #13  
    What I would really like to know is how to print the response in the log viewer or in a text plane in the app so I can test it and play around with the Ajax requests until I know what I am doing.
    "Life is Hard... it's harder if your stupid"
    - John Wayne

Posting Permissions