locked
WinJS.xhr not working in background task??? RRS feed

  • Question

  • I am using WinJS.xhr to fetch a small JSON  object (about 1-2kb) from web in a background task and then display some of its contents in a toast notification.

    I am sure that the background task is is properly registered because when i just let it display toast notifications (without WinJS.xhr), it works fine.

    But when i do the same thing (just showing toast notification), but wrapped in a WinJS.xhr (even WITHOUT any data processing, just for testing), it doesn't work at all.

    Here is the simplified code:

             // this works perfectly and displays the toast
            showMyToast();  
    --------------------------------------------------
            //this completely fails, all is same, just wrapped in winJS.xhr     
            WinJS.xhr({ url: myURL }).done(
                   function completed(request) {
                        showMyToast();
                   },
                   function error(request) {
    
                   },
                   function progress(request) {
    
                   }
           )
        


    • Edited by Dejan77 Monday, December 10, 2012 10:35 PM
    Monday, December 10, 2012 10:34 PM

Answers

  • Ok i'm stupid, my bad...

    Here's what i did wrong, in case someone else has the same problem in the future:

    In my background task i was actually calling the close() OUTSIDE of WinJS.xhr -_- So close() was actually being called before WinJS.xhr even finished (because WinJS.xhr is a promise... ). Solution is just to make sure that you move close() into the completed() function, like this

    //change from this:
     WinJS.xhr({ url: myURL }).done(
                   function completed(request) {
                        showMyToast();
                   },
                   function error(request) {
    
                   },
                   function progress(request) {
    
                   }
           )
    
     close();
    
    //to this:
     WinJS.xhr({ url: myURL }).done(
                   function completed(request) {
                        showMyToast();
                        close(); // close moved inside the promise
                   },
                   function error(request) {
    
                   },
                   function progress(request) {
    
                   }
           )
    
    


    • Marked as answer by Dejan77 Monday, December 10, 2012 11:45 PM
    • Edited by Dejan77 Monday, December 10, 2012 11:45 PM
    Monday, December 10, 2012 11:45 PM

All replies

  • Ok i'm stupid, my bad...

    Here's what i did wrong, in case someone else has the same problem in the future:

    In my background task i was actually calling the close() OUTSIDE of WinJS.xhr -_- So close() was actually being called before WinJS.xhr even finished (because WinJS.xhr is a promise... ). Solution is just to make sure that you move close() into the completed() function, like this

    //change from this:
     WinJS.xhr({ url: myURL }).done(
                   function completed(request) {
                        showMyToast();
                   },
                   function error(request) {
    
                   },
                   function progress(request) {
    
                   }
           )
    
     close();
    
    //to this:
     WinJS.xhr({ url: myURL }).done(
                   function completed(request) {
                        showMyToast();
                        close(); // close moved inside the promise
                   },
                   function error(request) {
    
                   },
                   function progress(request) {
    
                   }
           )
    
    


    • Marked as answer by Dejan77 Monday, December 10, 2012 11:45 PM
    • Edited by Dejan77 Monday, December 10, 2012 11:45 PM
    Monday, December 10, 2012 11:45 PM
  • Hello Dejan,

    I am getting WinJS not defined error in background task.

    Please help in resolving this issue

    Friday, April 25, 2014 5:56 AM
  • U need to define your WIN js libraries...for example.

    self.importScripts("//Microsoft.WinJS.2.0/js/base.js");

    Friday, August 22, 2014 12:19 AM