locked
jQuery.ajax(...) updating a listview

    Question

  • Hello everyone,

    I have the following code:

    jQuery.ajax({ type: "GET", url: "url", data: { keyID: keyID, vCode: vCode }, dataType: "xml" })
                .success(function (xml) {
                    var response = $(xml);
                    var data = [];
                    // parse response and populate data with:
                    // data[0] = {username: "johndoe", password: "password"};
                    // data[1] = {...}
                    // ...
                    WinJS.Namespace.define("UserData", {data: new WinJS.Binding.List(data)});
                });

    I know that the function argument to success is called and that data does get filled with the correct data but I get an error: "Unable to get property 'dataSource' of undefined or null reference." I imagine that this error is called because WinJS.Namespace.define is called after the HTML is loaded and WinJS.processAll is called. If that is the case, can anyone show me how to load a listview with the xml result using jQuery.ajax correctly?
    Sunday, December 16, 2012 8:47 PM

All replies

  • Hi,

    Please use winjs.xhr to do that. The example as follow:

    var feedItems = [];
    
    
        function loadBlogFeedsFromWebAsXML() {
    
            var feedUrl = "http://blogs.msdn.com/b/windowsstore/rss.aspx";
    
            WinJS.xhr({ url: feedUrl }).then(
            function (result) {
    
                var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
                xmlDoc.loadXml(result.responseText);
    
                var nodes = xmlDoc.selectNodes("//item");
    
                var items = new Array();
    
                nodes.forEach(function (val, idx, travObj) {
    
                    var title = val.selectSingleNode("title").innerText;
                    var description = val.selectSingleNode("description").innerText;
                    var link = val.selectSingleNode("link").innerText;
    
                    var newItem = {
                        title: title,
                        summary: description,
                        uri: link
                    };
    
                    items.push(newItem);
                });
    
                DataUtils.feedItems = items;
    
    
                // Call other function which can use the DataUtils.feedItems to bind UI elements
                        },
            function (err) {
            }
            );
    
            return DataUtils.feedItems;
        }
    
    
    
    
    
        WinJS.Namespace.define(
            "DataUtils", {
                feedItems: feedItems
            }
            );


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 6:52 AM
  • Thanks for the response.

    How do I use a function to bind DataUtils.feedItems to my UI elements?

    And DataUtils is only defined from the call to WinJS.Namespace.define(), right?

    One more thing, can you give me a link to an example or anything of using WinJS.xhr? In particular I was wondering how to use it to send GET and POST requests with actual data.

    • Edited by tanbla Monday, December 17, 2012 4:33 PM
    Monday, December 17, 2012 4:31 PM