locked
Win 8 App - when page reopens, page not requesting web service which is calling from jquery on onload event of page

    Question

  • I am creating win 8 app of accessing web service data and display on html page.

    e.g.1st page PAGE1.html accessing the data from web service using jquery + ajax .When first time PAGE1 opens the data properly getting from web service ,and I opens next page PAGE2.html from that PAGE1.html on button click event of and calling window.location("PAGE2.html)".But when I come back to my PAGE1.html by same method calling window.location("PAGE1.html"),this PAGE1.html opens but not calling web service again i.e. page not requesting web service. PAGE1.html shows data of 1st time opens not new data.


    Prashant Dikonda

    Wednesday, January 16, 2013 5:25 AM

Answers

  • What does debugging tell you?

    Did you hit that line of code?  Is the XHR code executed?  What does a Fiddler or Netmon trace tell you?  Did the call execute?  Did different data come back on the network trace?


    Jeff Sanders (MSFT)

    Wednesday, January 16, 2013 1:29 PM
    Moderator
  • Hi,

    Due to WinJs.xhr is a asynchronous function. Please make sure put the WinJS.Navigation.navigate code in success function.

    For example:

    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;
    
    
                // put code here
    
            },
            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 Friday, January 18, 2013 1:42 AM
    Thursday, January 17, 2013 7:41 AM

All replies

  • Hi,

    I suggest you using WinJs.xhr to do that. You can use this function in Windows Store apps built for Windows using JavaScript for cross-domain requests and intranet requests if you set the capabilities of your app to allow these operations.

    More details, please refer to: http://msdn.microsoft.com/en-us/library/windows/apps/br229787.aspx .

    And demo as follow:

     function CallData() {
            var person = { 'Name': 'Name'};
            WinJS.xhr({
                type: "POST",
                url: "http://www.xxx.com/WebService.asmx/method",
                headers: { "Content-type": "application/json" },
                data: JSON.stringify(person)
            }).then(function complete(request) {
                var resdata = request.responseText;
            }, function error(er) {
                var err = er.statusText;
            });
        }


    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.

    Wednesday, January 16, 2013 8:56 AM
  • Hi Song,

    I used WinJS.xhr and to navigate  WinJS.Navigation.navigate("PAGE1.html").But by using this also I am not getting what I want.

    In PAGE1.html I am showing data from some database through web service and I am going to PAGE2.html by button click and entering some data and store on that database which access by PAGE1.html through web service.And when I am submitted data and navigate to PAGE1.html ,the PAGE1 not showing refresh data of database.



    Prashant Dikonda

    Wednesday, January 16, 2013 9:44 AM
  • Hi Prashant,

    Could you share some code and create a demo to demonstrate. So I can reproduce on my side.


    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.

    Wednesday, January 16, 2013 9:48 AM
  •                  

    Hi Song,
    Here is code of PAGE1.html of PAGE1.js:Data properly getting from web service on onload function of page.
       ready: function (element, options) {

    var textDisp= document.querySelector("#text");         /*text is DIV id to display data*/
            textDisp.innerText = "PAGE2";


     document.querySelector("#myButton").onclick = function (args) {
                    WinJS.Navigation.navigate("/pages/page2/page2.html");
                };
    WinJS.xhr({
                        type: "GET",
                        url: "http://MyWebserice",
                        headers: { "Content-type": "application/json" },
                    }).then(function complete(request) {
                        var resdata = request.responseText;
                        textDisp.innerText = resdata;
                    }, function error(er) {
                        var err = er.statusText;
                    });
    }

    Data properly submitted to databse through web service :
    Code of PAGE2.html of PAGE2.js:
     document.querySelector("#Submit").onclick = function (args) {
    WinJS.xhr({
                        type: "POST",
                        url: "http://InsertData?username=" + un + "&password=" + pass + "",
                        headers: { "Content-type": "application/json" },
                    }).then(function complete(request) {
                                          WinJS.Navigation.navigate("/pages/PAGE1/PAGE1.html");
                        }, function error(er) {
                        var err = er.statusText;
                    });
    }

    But PAGE1 not showing refresh data of database after Successfully added data, I called   WinJS.Navigation.navigate("/pages/PAGE1/PAGE1.html") this statement in then of WinJS.xhr to go to PAGE1.html.


    Prashant Dikonda

    Wednesday, January 16, 2013 10:07 AM
  • What does debugging tell you?

    Did you hit that line of code?  Is the XHR code executed?  What does a Fiddler or Netmon trace tell you?  Did the call execute?  Did different data come back on the network trace?


    Jeff Sanders (MSFT)

    Wednesday, January 16, 2013 1:29 PM
    Moderator
  • Hi jpsanders,

    I am new to Win 8 App development.

    I checked on Fiddler debugger.When my App executing then 1st time PAGE1.html page load and the XHR code is executed on onload event i.e. my web service called and proper data is coming from web service.

    But when I go to my second page PAGE2.html and coming back to PAGE1.html from PAGE2.html by clicking button then my XHR code not executed on onload event i.e. my web service not executed when I come back to PAGE1.html from PAGE2.html.


    Prashant Dikonda

    Wednesday, January 16, 2013 2:07 PM
  • Hi Prashant,

    I review your code and have some suggestions.

    First, the code in PAGE1.js. Generally, there should use "Post", and you can transfer some values to webservice.

    Second, the code in  PAGE2.js. Generally, there should use "GET". Because I see you transfer the value in URL, I think "GET" will be ok.

    Last, you can debug with VS2012. And check the error.

    #Quickstart: Debugging apps (JavaScript)

    http://msdn.microsoft.com/en-us/library/windows/apps/hh441474.aspx


    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.

    Thursday, January 17, 2013 2:29 AM
  • Hi Song Tian,

    As you said I debugged my App with vs2012.And simultaneously I checked on Fiddler web debugger.From this I am getting issue as:

    When I execute my App ,the xhr code of Page1.html executing and web service also calling and getting proper data from web service .But when I am going to another page i.e. Page2.html and coming back to my Page1.html ,then the xhr code executing but my web service not calling and And my xhr Complete method is executing and getting old data.

    Thank you


    Prashant Dikonda

    Thursday, January 17, 2013 7:02 AM
  • Hi,

    Due to WinJs.xhr is a asynchronous function. Please make sure put the WinJS.Navigation.navigate code in success function.

    For example:

    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;
    
    
                // put code here
    
            },
            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 Friday, January 18, 2013 1:42 AM
    Thursday, January 17, 2013 7:41 AM