1

I am a prototype newbie and am unclear on how to process multiple json objects returned. For example I would like to return a simple JSONObject map indicating the success/failure and also return a JSONArray that I can index and build an select:options from. Now each json object will be upt in the header with a different name. How do I parse that out client-side and alert on a failure name/value else build the select:option element? tia.

1 Answer 1

3

Suppose your /someurl service sends a reply like this:

{
  "status": "ok",
  "data":["apples", "oranges", "bananas"]
}

What I have done here is to combine the two objects into a single object. In the client you can handle it something like this.

new Ajax.Request('/someurl', {
  method:'get',
  requestHeaders: {Accept: 'application/json'},
  onSuccess: function(transport){
    var json = transport.responseText.evalJSON();
    if(json.status != 'ok')
    {
       alert('status "'+json.status+'" not ok')
       return; // or throw a fit
    }
    json.data.each(function(elt){
      alert(elt); // or display it, whatever
    });
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.