We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I'm trying to allow the user the enter a postcode and then retrieve the list of properties in the area with the Zoopla api. Unfortunately, I get the following error on line 51 and 59, any ideas?
TypeError: xml is undefined[Learn More]
It's working fine in Processing.
Thanks in advance, Charles
var xml, postInput, postButton, myDiv, cnv, postCode;
var running;
function setup() {
running = false;
cnv = createCanvas(windowWidth * 0.1, windowHeight / 2);
var y = (windowHeight / 2) - 25;
cnv.position(10, y);
cnv.style("z-index", "-1")
textAlign(CENTER);
textSize(16);
myDiv = createDiv("Property Analyser");
myDiv.position((windowWidth / 2) - 100, windowHeight * 0.40);
myDiv.style("font-size", "24px");
myDiv.style("color", "#FFFFFF");
myDiv.style("z-index", "2")
postInput = createInput();
postInput.position(windowWidth * 0.5 - (windowWidth * 0.1), windowHeight * 0.5);
postInput.style("z-index", "2")
postInput.size(windowWidth * 0.2, windowHeight * 0.05);
postButton = createButton('Input Postcode');
postButton.size(windowWidth * 0.1, windowHeight * 0.04);
postButton.position(windowWidth * 0.5 - windowWidth * 0.05, windowHeight * 0.57);
postButton.mousePressed(next);
postButton.style("z-index", "2")
}
function draw() {
if (running) {
background(255);
}
}
function next() {
postCode = postInput.value();
postButton.style("visibility", "hidden");
postInput.style("visibility", "hidden");
myDiv.style("z-index", "-1");
cnv.style("z-index", "1")
running = true;
console.log(postCode);
var url = "http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=" + postCode + "&api_key=bmm77zppverakbnfnmtyuky3";
xml = loadXML(url, printHouses(), message());
}
function message(message){
console.log(message);
}
function printHouses(){
var listings = xml.getChildren("listing");
for (var i = 0; i < listings.length; i++) {
var listing = listings[i].getChild("agent_address");
console.log(listing.getContent());
}
}
Answers
http://p5js.org/reference/#/p5/loadXML
@ line #51
xml = loadXML(url, printHouses(), message());
You're passing
undefined
as argument for both success & error callbacks! :-SSAll functions finishing w/o
return
implicitly result inundefined
btW.You're supposed to pass the reference for a
function
for the callback parameters.Instead you're invoking them w/
()
: :-\"xml = loadXML(url, printHouses, message);
That seems be a step in the right direction however the error message function gets executed and prints the following message:
After checking with the with the Zoopla reference it doesn't seems to make much sense. Furthermore the data gets received fine when entered in the browser.
Here's the updated code:
1st, make sure you're running your code under a server (be it local or remote):
https://GitHub.com/processing/p5.js/wiki/Local-server
Or if you prefer, just use some Firefox-based browser in order to run it instead. *-:)
If you still got CORS exception for it, loadXML() alone can't do anything for you anymore. :(
Your last options would be to request the site's admin to authorize CORS for you.
Or hack your way thru' via some CORS proxy site, like http://CrossOrigin.me for example. :ar!
For further details, read old forum threads about it: :-B
https://forum.Processing.org/two/discussions/tagged/cors
Thanks for your help but I'm not having much success.
https://GitHub.com/processing/p5.js/issues/1752
http://developer.Zoopla.com/forum/read/209357
Indeed your Zoopla site is denying CORS. Thus we can't load the ".xml" file from it. :-&
I've even tried http://CrossOrigin.me as a CORS proxy for it; but it didn't work out either: :(
http://p5ide.HerokuApp.com/editor#?sketch=5873a448c6a3ab040078c16d
https://GitHub.com/technoboy10/crossorigin.me/issues/71
Thanks for your perseverance GoToLoop I've just sent them an email.
Will update this post when I've found the solution :)
Got good news! Changed CORS proxy. B-)
From http://CrossOrigin.me/ to http://CORS-Anywhere.HerokuApp.com/.
And now it's working! <:-P
Seems like some extra protections from the former were getting in the way somehow. 8-|
But real fix is for the non-CORS site to activate CORS for its ".xml" files; so no need for proxy hacks. :ar!
http://CodePen.io/GoSubRoutine/pen/Qdjmrm/top/?editors=0012
Incredible! :o
Thank you very much!
Ps: What's the purpose of "use strict"; ?
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode