views:

134

answers:

2

Hi Guys,

I am using Dojo and making a AJAX call to a JAVA Class and trying to get the output of the program to a Alert box to the client.

            var showResult = function(result){
            console.log("Showing Result()");
            var store = new dojo.data.ItemFileReadStore({ data: result});
            console.dir(store);

            store.fetch( { onItem: function(data) {  alert("Hie"); },
                            onError: function(error,request){ alert("ERROR");}
            });

        };  

This is my code, showResult basically is call back function from xhr request. I can see console.dir(store) printed onto Firebug but the fetch function always returns the onError block.

My store array is of the form {info="Test Message"} and I need to retrieve "Test Message" and display it in a Alert box. Any help?

A: 

If the result is just an array, you should use new dojo.data.ItemFileReadStore({data : {items : result}}) to create the store.

For example, result is [{info : "Test Message 1"}, {info : "Test Message 2"}], then the code should be:

var store = new dojo.data.ItemFileReadStore({data : {items : result}});
store.fetch({
    onItem : function(item) {
        alert(store.getValue(item, "info"));
    },
    onError : function(error) {
        alert("Error!");
    }
});
Alex Cheng
Thanks for the constructor suggestion. It does seem perfect! Although, I still get "Error! alert boxes. So, the onError is getting executed! Any ideas?
Sunny
I think the reason is that you have JavaScript error in the onItem handler, which triggers the onError handler. Use simple logic in onItem first to make sure the store is created correctly. Then debug your onItem handler to find the errors.
Alex Cheng
A: 

try store.fetch( { query: {info: '*'}}, onItem: function(data) { alert("Hie"); }, onError: function(error,request){ alert("ERROR");} });

as well you may want to see if onComplete : function(items){console.log(items);} works instead of onItem, it is worth a try.

As well console.log your error so that you can see what the issue is.

A few other things does you store have an identifier and label set?

kls