开发者

prototype js parsing multiple json objects

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.


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
    });
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜