AJAX responseXML

i have a problem regarding the responseXML of ajax.. I have this code from my callback function:

var lineString = responseXML.getElementsByTagName('linestring')[0].firstChild.nodeValue;

However, the linestring can only hold up to 4096 characters max.. the remaining characters are rejected.

I dont know what to use to get all the values that the lineString returns. its quite a big data thats why I thought of using the responseXml of AJAX, BUT turned out it still cannot accomodate everything.

My linestring consists of lines from a logfile which I concatenated and just put line separator. I need to get this data in my form so that is why after reading from the php, i send it back via AJAX

Do you have suggestions guys.

XML adds a lot of extra markup for most ajax requests. If you are expecting some kind of list with data entities, sending them in a JSON format is the way to go.

I used JSON to get quite huge arrays with data.

First of all, JSON is just Javascript Object Notation meaning that the Ajax Request would request a String which will actually be evaluated as a Javascript object.

Some browsers offer support for JSON parsing out of the box. Other need a little help. I've used this little library to parse the responseText in all webapps that I developed and had no problems with it.

Now that you know what JSON is and how to use it, here's how the PHP code would look like.

$response = [
   "success" => true, // I like to send a boolean value to indicate if the request 
                      // was valid and ok or if there was any problem. 
   "records" => [
     $dataEntity1, $dataEntit2 //....
   ]
]; 

echo json_enconde($response );

Try it and see what it echos. I used the php 5.4 array declaration syntax because it's cool! :)

When requesting the data via Ajax you would do:

var response
    ,xhr = getAjaxObject(); // XMLHttp or ActiveX or whatever.
xhr.open("POST","your url goes here");
xhr.onreadystatechange=function() {
   if (xhr.readyState==4 && xhr.status==200) {
     try { 
       response = JSON.parse(xhr.responseText);
     } catch (err) {
       response = {
          success : false,
          //other error data
       };
     }

     if(response.success) {
        //your data should be in response
        // response.records should have the dataEntities
        console.debug(response.records); 
     }
   } 
}

Recap:

  1. JSON parsing needs a little help via JSON2 library
  2. PHP can send maps as JSON
  3. Success boolean is widely used as a "successful/unsuccessful" flag

Also, if you're into jQuery, you can just set the dataType : "json" property in the $.ajax call to receive the JSON response in the success callback.