WinJS XHR in background task

Recently I set about the task of developing a Windows Store app for this blog. It was a learning exercise for myself to get some more experience of the procedure of developing and submitting a windows store app, and to get some more experience with the WinJS library. While developing this I was looking at a background task to update my live tile. This included using WinJS XHR in background task. I performed the process of fetching my data from the web and processing exactly the same as I did in the app, however it failed to work. When I performed my XHR request, the response object had a responseXML of null. The return contents was not being processed as XML no matter what I tried (it worked fine when the same code was run in my app).

I reported this on StackOverflow to see if anyone had ideas, and was pointed in the direction of this forum post on MSDN forums. After a bit of further discussion in the forum and supplying my source code, I got the following response:

Hi CodePB,

This appears to be a bug.  Please use the workaround I specified earlier!


So what was the workaround specified earlier? This was actually very simple:

This does the job of now providing us with an XML document we can use. However this XML document does not support methods such as querySelector() , and the XML nodes don’t support textContent. Instead you have to use getElementsByTagName()  etc.

How did I integrate this into my code? I simply checked the responseXML, and if it was null I used the workaround. I then didn’t use querySelector()  anymore, instead using  getElementsByTagName() or the equivalent method. Finally I provided a helper method that checked whether the XML node supported textContent, and if not used innerText to extract the text. This may have been overly complicated, and I could have just switched everything to using  Windows.Data.Xml.Dom.XmlDocument() , however I felt I wanted to take advantage of the responseXML wherever I could (If the processing has been done, why do it again).

Leave a Reply