locked
How to convert jquery ajax call to WinJS.xhr or Win8 equivalent RRS feed

  • Question

  • I have a question regarding porting iOS/Android app to Win8.  I am currently using a modified version of http://srobbin.github.com/jquery-pageslide/ with an ajax call to load a menu.html page on the side.  Essentially what I'm looking to do is go to the url of the file with the html for the menu, ie menu.html and populate $('#menu') with it via a html call.  This is my first time messing with XHR/AJAX so please forgive any ignorance.

    The AJAX call looks like the following...


    $.ajax({
            url: $str,
            dataType: "text", 
            success: function(data) {         
    $("#menu").html(data);

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
              }
        });


    In Win8, I am receiving this error..

            Exception was thrown at line 8490, column 29 in ms-appx://664ace5f-4774-4928-bc94-ad4650a1ede1/js/jquery-1.8.2.js
        0x80070005 - JavaScript runtime error: Access is denied.


    This is where it breaks in the jQuery code.

            if (s.username) {
                                xhr.open(s.type, s.url, s.async, s.username, s.password);
                            } else {
                                xhr.open(s.type, s.url, s.async); //<-------------------
                            }




    I have redone the jquery call to using the WinJS.xhr... as in the following code.

    WinJS.xhr({
       type: "GET",
       url: $str,
                responseType: "text",

    }).then(function(success){
       console.log(success);
    },
            function (error) {
                console.log(error);    
    }
            );

    This still returns an access is denied exception.  On this piece of base java code..

        req.open(
                            options.type || "GET",
                            options.url,
                            // Promise based XHR does not support sync.
                            //
                            true,
                            options.user,
                            options.password
                        );

    The exception is documented here.

        SCRIPT5: Exception is about to be caught by JavaScript library code at line 2332, column 21 in ms-appx://microsoft.winjs.1.0/js/base.js
        0x80070005 - JavaScript runtime error: Access is denied.

    If the file name/path ($str) is altered to an incorrect path then I get a resource_not_found error, which is self explanatory.  I am not sure what to try now to get this call to work.  Anyone have a solution?

                   
    Thursday, January 10, 2013 10:03 PM

Answers

  • Hi,

    Please refer to 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.

    • Marked as answer by Song Tian Monday, January 14, 2013 3:45 AM
    Friday, January 11, 2013 7:19 AM
  • Able to get it working via...


    WinJS.xhr({ url: '/menu.html', responseType: "document", }).then(function(xhr){ var temp = xhr.response.childNodes[0].innerHTML; $('#menu').html(temp); } );
    • Marked as answer by MTAG11 Friday, January 11, 2013 3:22 PM
    Friday, January 11, 2013 3:21 PM

All replies

  • Hi,

    Please refer to 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.

    • Marked as answer by Song Tian Monday, January 14, 2013 3:45 AM
    Friday, January 11, 2013 7:19 AM
  • Able to get it working via...


    WinJS.xhr({ url: '/menu.html', responseType: "document", }).then(function(xhr){ var temp = xhr.response.childNodes[0].innerHTML; $('#menu').html(temp); } );
    • Marked as answer by MTAG11 Friday, January 11, 2013 3:22 PM
    Friday, January 11, 2013 3:21 PM