Skip to content

Instantly share code, notes, and snippets.

@z-------------
Last active August 29, 2015 14:02
Show Gist options
  • Save z-------------/e231b658ce4844327662 to your computer and use it in GitHub Desktop.
Save z-------------/e231b658ce4844327662 to your computer and use it in GitHub Desktop.
Example usage of simpleJSONP.js
// using simpleJSONP.js
// https://gist.github.com/z-------------/7389e7708a38021cd25e
/* output looks like this (just an example, not actual output)
data: {
status: 200,
weather: {
currentWarnings: [{
title: "Red Rainstorm Signal",
text: "Red rainstorm signal is currently in effect"
},{
title: "Tsunami Warning",
text: "Tsunami warning is currently in effect"
}]
},
url: "http://weather.blah.hk/whatever/warnings.xml"
}
*/
var warningImageMap = {
"Red Rainstorm Signal": "red-rain.png",
"Tsunami Warning": "tsunami.png"
// etc
}
jsonp("http://weather.blah.hk/whatever/warnings.xml",function(response){
var jsonResponse = xmlToJSON(response);
var warnings = jsonResponse.data.weather.currentWarnings;
for (i=0; i<warnings.length; i++) {
var warningImg = document.createElement("img");
warningImg.src = warningImageMap[warnings[i].title];
document.querySelector("#warnings").appendChild(warningImg);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment