views:

11180

answers:

4

Hello. I'm generating JSON response from PHP witch looks like this:

{ done:'1', options: [{ message:'Example message'},{message:'This is the 2nd example message'}]}

I want to grab these results using ExtJS. This is what I have so far:

Ext.Ajax.request({
    loadMask: true,
    url: 'myfile.php',
    params: {id: "1"}
    });

What do I have to write next to get the json results like this:

var mymessages = jsonData.options;

And mymessages should contain Example message and This is the 2nd example message.

Thank you.

+9  A: 

The straightforward approach:

Ext.Ajax.request({
  loadMask: true,
  url: 'myfile.php',
  params: {id: "1"},
  success: function(resp) {
    // resp is the XmlHttpRequest object
    var options = Ext.decode(resp.responseText).options;

    Ext.each(options, function(op) {
      alert(op.message);
    }
  }
});

Or you could do it in a more Ext-ish way using Store:

var messages = new Ext.data.JsonStore({
  url: 'myfile.php',
  root: 'options',
  fields: [
    {name: 'text', mapping: 'message'}
  ],
  listeners: {
    load: messagesLoaded
  }
});
messages.load({params: {id: "1"}});

// and when loaded, you can take advantage of
// all the possibilities provided by Store
function messagesLoaded(messages) {
  messages.each(function(msg){
    alert(msg.get("text"));
  });
}

One more example to address the last comment:

var messages = [{title: "1"},{title: "2"},{title: "3"}];

var titles = msg;
Ext.each(messages, function(msg){
  titles.push(msg.title);
});
alert(titles.join(", "));

Although I would prefer doing it with a Array.map (which isn't provided by Ext):

var text = messages.map(function(msg){
  return msg.title;
}).join(", ");
alert(text);
Rene Saarsoo
It works but I need to access all messages from the options "category". I can do a jsonData.done and I get "1" as a response witch is good but how would I grab all "message" texts ?
Manny Calavera
expanded my example to include showing all messages.
Rene Saarsoo
Thanks, works great! On more thing, if I may: how can I gather all messages and show them in Ext.Msg.alert ?
Manny Calavera
here you have...
Rene Saarsoo
I get a "messages.map is not a function" error on the Array.map example by Firebug. Is there something I'm missing ? Thanks and sorry for the mess, but I'm a beginner.
Manny Calavera
Nevermind, I got it to work. Thank you very much!
Manny Calavera
As I sayd, map() is not provided by Ext. But you can use some library that defines it, like ext-basex: http://code.google.com/p/ext-basex/
Rene Saarsoo
Late, question, please! Is there any chance now to grab the value of "done" from JSON ? I mean in the same JsonStore. Basically, I need to know if(done == "1") or done == "0". Thanks.
Manny Calavera
You can access it through store.reader.jsonData.done. Although that's quite hackish way to do it. Maybe in your case it would be better to use Ajax.reques(). I don't really know. You could investigate things like JsonReader.successProperty and Store.loadexception.
Rene Saarsoo
Thank you! It works. What do you mean by "quite hackish" ? What can happen using this method ? It's not safe ?
Manny Calavera
The reader property of Store is not documented. So relying on that is not future-proof.
Rene Saarsoo
+3  A: 

Use the success and failure properties:

Ext.Ajax.request({
    loadMask: true,
    url: 'myfile.php',
    params: {id: "1"},
    success: function(response, callOptions) {
       // Use the response
    },
    failure: function(response, callOptions) {
       // Use the response
    }
});

See the Ext API docs for more details

Kevin
This code is missing a comma after _params: {id: "1"}_
Spike Williams
Also, that last comma, after the failure function, is going to cause an error in Internet Explorer. Otherwise, a useful template :)
Spike Williams
Fixed, thank you
Kevin
+1  A: 

if you are sure that your input is correct (beware of xss attacks) you can use the eval() function to make your javascript object from your json result, which can then be accessed through your command:

var mymessages = jsonData.options;

But then again, Ext does that nicely for you, as Rene has pointed out through the Ext.decode function

m_oLogin
A: 

Thank you so much Manny. My problem is solved

                       ---- Pramod
Pramod