We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I need to get a value from a web page and use this in my p5.js sketch. I do this by using the loadXML command, but it does not work. What I want to achieve is getting a string with the source code of the web page, so that I can get the value out of this string. I tried to do this with the following code:
function setup() {
createCanvas(200, 200);
var url = 'http://creators.ewi.utwente.nl/site/s1712187/SmartPad/';
loadXML(url, callback, error);
}
function draw() {
}
function callback() {
print("it is callback");
}
function error() {
print("it is error");
}
"It is error" is the line that is printed when I run the sketch. Who knows what my mistake is?? Of course the code should be extended by creating a string and reading the data etc. but first the loadXML function should load the html source code properly.
(The only examples that I can find are based on JSON instead of XML.)
Answers
I've tried a tweaked version of your code here:
http://p5ide.HerokuApp.com/editor#?sketch=56f56352df864f0300d25ca5
Hitting F12 in the browser, I could spot this error message:
Therefore it's CORS related. We don't have permission to access that resource from that URL.
You can try out your luck w/ a CORS proxy though:
https://GitHub.com/technoboy10/crossorigin.me
When uploading this code, again the error callback is executed instead of the success callback. Also, running the code at http://p5ide.herokuapp.com/editor#?sketch=56f56352df864f0300d25ca5 does result in a red screen :/
That's what I've said: I had the same error even w/ my tweaked version! ^#(^
A possible workaround is the CORS proxy link I've posted above. :-\"
Sorry haha. But when implementing that in my code, I get this error: 22532: Uncaught TypeError: Cannot read property 'documentElement' of null.
Is it even possible to load a HTML page using loadXML?
I believe so, given HTML is based on XML.
Have you tried & read the instructions from https://GitHub.com/technoboy10/crossorigin.me already?
I added http://crossorigin.me to my url and then I got the error mentioned before. Don't know what else I could do with this link you sent
Can you post your latest attempt? :-$
Sure, didn't change that much though
I've modified the online example to include CrossOrigin.me too:
http://p5ide.HerokuApp.com/editor#?sketch=56f598faebe33e03002c17c2
This time the console error is:
I dunno what else to help ya out, sorry. However you can still post this issue here:
https://GitHub.com/processing/p5.js/issues
Yeah I got the same error in my sketch. Thanks anyway ;)
https://GitHub.com/processing/p5.js/issues/1325
I found the problem. It had nothing to do with cross-policy. The problem is that when testing the p5 sketch, you run it at the local host. Appearently, this local host cannot load the webpage you are requesting data from. So when you want to test the sketch, you need to upload it to a webpage which is not local, and then it should work!! It worked for me. The sketch above works fine, just not in the local server!
http://p5ide.HerokuApp.com/editor#?sketch=56f598faebe33e03002c17c2 isn't a local host.
Regardless http://p5ide.HerokuApp.com was not authorized to grab resources from http://creators.ewi.utwente.nl/site/s1712187/SmartPad.
Au contraire, it's all about CORS! Either you had it uploaded to http://creators.ewi.utwente.nl itself or the URL host had authorization to grab resources from the former.
I mean, when pressing play in p5.js editor, the sketch is uploaded to the local host. I did not mean "you" personally ;)
Oh, and I used loadStrings instead of loadXML, maybe that's the reason why your sketch didn't work ;)
You're using loadStrings to get a value from a webpage? :-&
Wait - are you actually using a webpage to transfer data to your sketch? :(|)
Either way just seems plain wrong... If you can implement it, the best approach would be to serve your data in JSON format in a .json file. That's very easily parsed by JavaScript and at a push you can using JSONP to get around cross-origin issues....
The problem is that I don't have control over the webpage. It is html and I have to deal with that.. Any idea on how to do that without loadStrings?
TBH in the case of the page you're pulling from you can get away with it ;)
It is generally a very bad idea to treat HTML as a string and to try and extract data via String methods. Instead it's better to generate a DOM model and inspect that with DOM methods like querySelector...