On 08/04/11 01:06, bootle wrote:


When I use Request.JSON to access HTML content (deliberately), let's

Why use Request.JSON for a response that isn't JSON? I have found that it fails to return too.

I used to use Request.JSON for quite a lot, but I have recently started to use just Request and to get my server scripts (in PHP) to return a more xml like response.

You can use mootools element objects to parse the code -- below is an example inside an application where I am continually reading streaming requests from a game where the server is passing the write requests from the opponent. It has some debug code to check that the requests are coming in sequence in an element of the form

<message time="123456789" count="5">message from opponent</message>

and the server will indicate an error by returning completely different xml

<error>Error message</error>

if it fails. The relevant javascript is (note use of chain because I requeuing a request in the onSuccess function)

reader = new Request({
  url:'read.php',
  link:'chain',
  onSuccess: function(html) {
    window.clearTimeout(readTimerID);
    readTimerID = readTimeout.delay(readTimeoutValue);
    var holder = new Element('div').set('html',html);
    if(holder.getElement('error')) {
      messageBoard.appendText(holder.getElement('error').get('text'));
    } else {
      var m = holder.getElement('message');
      if(m) {
        var c = m.get('count');
if(++counter != c) messageBoard.appendText(' [C:'+counter+':'+c+']'); //Should catch out of sequence counts
        messageCallback(m.get('time').toInt(),m.get('text'));
      }
    }
    reader.post({oid:oid,ahv:ahv}); //Queue up next request
  }
});


the relevent php (there are other error checks that are further back up the code that are not included) that writes the response to this read request is:-

if(strlen($response) > 0) {
  list($n,$msg,$count) = explode('$',$response);
  echo '<message time="'.$time.'" count="'.$count.'">'.$msg.'</message>';
} else {
 echo '<error>zero length response = "'.$response.'"</error>';
}


--
Alan Chandler
http://www.chandlerfamily.org.uk

Reply via email to