parsing text from responseText

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpjd
    New Member
    • Mar 2007
    • 25

    #1

    parsing text from responseText

    I am using this code trying to parse responseText
    Code:
    document.getElementById('test').innerHTML=http.responseText 
    
    =<span id="test"></span>
    but I am getting the this error:
    Object doesn't support this property or method. I have tried using both eval() and split() but I have not beed able to parse the string text from responseText.

    Any inspired ideas would help.

    RPJD
  • rpjd
    New Member
    • Mar 2007
    • 25

    #2
    Originally posted by rpjd
    I am using this code trying to parse responseText
    Code:
    document.getElementById('test').innerHTML=http.responseText 
    
    =<span id="test"></span>
    but I am getting the this error:
    Object doesn't support this property or method. I have tried using both eval() and split() but I have not beed able to parse the string text from responseText.

    Any inspired ideas would help.

    RPJD
    I am having a problem with split() trying to parse responseText which I can view with
    Code:
    result = http.responseText;
    alert(result)
    I have 2 variables and 2 arrays so (var var array[] array[][]) being the output.
    I was hoping to parse result with
    Code:
    result  = http.responseText.split("|"); //giving (var | var | array[] | array[][])
    but this has no effect. I get the same output as the above alert. I was then hoping to further parse with
    Code:
    x = result[0];
    y= result[1];
    a[] = result[2].split(",");
    b[] =result[3].split(",");
    to no effect as x.length and result.length are the same. Can someone point me in the right direction?

    rpjd

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      Code:
      result  = http.responseText;
      result = result.split("|");

      Comment

      • rpjd
        New Member
        • Mar 2007
        • 25

        #4
        Originally posted by iam_clint
        Code:
        result  = http.responseText;
        result = result.split("|");
        Thanks iam_clint.
        Will give it a go

        rpjd

        Comment

        • rpjd
          New Member
          • Mar 2007
          • 25

          #5
          Originally posted by rpjd
          Thanks iam_clint.
          Will give it a go

          rpjd
          Both alerts give the same output
          Code:
          result = responseText;
          alert(result);
          result.split("|");
          alert(result);

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            lets see what result is set to then.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by rpjd
              Both alerts give the same output
              Code:
              result = responseText;
              alert(result);
              result.split("|");
              alert(result);
              Look at what iam_clint typed:
              Code:
              result = result.split("|");
              Then you have an array with items result[0], result[1], etc.

              Comment

              • rpjd
                New Member
                • Mar 2007
                • 25

                #8
                Originally posted by acoder
                Look at what iam_clint typed:
                Code:
                result = result.split("|");
                Then you have an array with items result[0], result[1], etc.
                I tried this method
                Code:
                result = http.responseText;
                result = result.split('|');
                numrows = result[0];
                alert(numrows);
                instead of getting the first element of the array in the alert, i got the whole array i,e, responseText. I'll crack it someday!

                rpjd

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  What is the value of responseText in this case? If split is giving you bad output, you may have bad input.

                  Comment

                  • gruvin
                    New Member
                    • May 2007
                    • 3

                    #10
                    Originally posted by rpjd
                    I am having a problem with split() trying to parse responseText which I can view with
                    Code:
                    result = http.responseText;
                    alert(result)
                    Howdy.

                    Seems to me you would benefit here by using JavaScript's rather cool JSON syntax (JavaScript Object Notation) for your respnseTexts, rather than all this complex split stuff you might be forced to use in a lesser language :P. Here's an example

                    If you wanted to post a response back containing, say, two strings and two arrays you have a few options. Here are two:

                    Option 1
                    Code:
                    responseText = "{ 
                       stringA: 'String One', 
                       stringB: 'String Two', 
                       arrayA: ['indexA0', 'indexA1'], 
                       arrayB: ['indexB0', 'indexB1'] 
                    }";
                    (I added newlines just for clarity... you'd need to leave those out if this were real javascript script of course)

                    So now you can eval that responseText like so:
                    Code:
                    var returnObject = eval('('+responseText+')');
                    var string1 = returnObject.stringA;
                    var string2 = returnObject.straingB;
                    var index0ofArrayB = returnObject.arrayB[0];
                    ...etc.
                    You can iterate over returnObject like this:

                    Code:
                    for (var name in returnObject) {
                       alert('returnObect element "'+name+'" is a "+ typeof returnObject[name]);
                    }
                    Option 2

                    OR... you can just use arrays with no objects like so:

                    Code:
                    responseText = "[  
                       'String One', 
                       'String Two', 
                       [ 'indexA0', 'indexA1'], 
                       [ 'indexB0', 'indexB1']
                    ]";
                    (Again, newlines added for clairty only)

                    Now you can eval that data the same way as above, but there's aray data and no objects this time:
                    Code:
                    var returnObject = eval('('+responseText+')');
                    var string1 = returnObject[0];
                    var string2 = returnObject[1];
                    var index0ofArrayB = returnObject[3][0];
                    ...etc.
                    JSON uses {...} to denote a named object and [...] to denote an array. You can nest objects inside objects, arrays inside arrays or objects or any which way! Example, an array of objects might look like:

                    Code:
                    JSONarrayOfObjects = "[ { one: '1', two: '2' }, "
                    		  +"{ one: '1a', two: '2a' } ]";
                    var anArray = eval("("+JSONarrayOfObjects+")");
                    There's bound to be a good tutorial on JSON somewhere online. No other language I'm aware of has such a powerful feature as this. I write all my Ajax style response servers to return JSON data -- it makes life SO easy.

                    Good luck!
                    Gruvin.

                    DISCLAIMER: I haven't tested the above code -- just typed it up here. There may be minor (though critical :) ) errors.

                    Comment

                    Working...