I am using a online tutorial to lean how to create a web service, generate a JSON object, send it back to my Win 8 App and display it. The web service is working however I am struggling to return a value to the APP. My code in the app is:
WinJS.xhr({
url: 'http://localhost/filmgloss/web-service.php?termID=1&format=JSON'
})
.done(
function complete(result) {
// terms is the key of the object
for (var terms in result) {
for (var term in terms) {
if (result.hasOwnProperty(term)) {
//here you have to acess to
var termName = result[term].termName;
var def = result[term].definition;
}
//Show Terms
testDef.innerText = definition;
}
}
},
And he code in my web service is:
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('terms'=>$terms));
}else...
The JSON output itself looks like:
{"terms":[{"term":{ "termName":"Focus","definition":"A Focus..."}}]}
I am using a for..in but whilst I can look inside terms' I can't work out how to look interm`
I have managed to resolve my issue with the help of a developer friend. My problem was that I had not realised that the result of
WinHS.xhrwas not already a JSON Array. Although my web-service outputs a JSON Array when it is consumed throughWinHS.xhrit appears to be returned as anXMLHttpRequestobject.The solution was therefore to process the result using:
JSON.parse(result.responseText)I could then use a
For...Inloop as expected:Thanks for everyone that commented, hopefully this may help others in the future if they’re starting out with Win 8 app development.