We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm trying to use a trafiklab.se API. The url im putting into loadJSON() works fine when put into the web browser, but with loadJSON() i get the error:
Script error.
5209: Uncaught TypeError: Cannot read property 'responseText' of undefined
This is the error shown in the node-webkit errorlog of the p5js browser:
Uncaught typeahead.json?key=MYAPIKEY&searchstring=slussen&callback=reqwest_144658152…:1
SyntaxError: Unexpected token :
p5.js:5209 Uncaught TypeError: Cannot read property 'responseText' of undefined
p5.js:5209 success
p5.js:5067 27.script.onload.script.onreadystatechange
And this is the code that loads the json
var url = "http:/ /api.sl.se/api2/typeahead.json?key=MYAPIKEY&searchstring=" + "slussen";
loadJSON(url, platsuppslag, "jsonp");
The syntax error thing made me think that the API response might be wrongly formatted somehow, but according to jsonlint it's all good.
This is only my second attempt at using an API, so I have no idea where to start with this, any help greatly appreciated :)
edit: the forums does something weird with the url, it looks fine in post preview but not in the post itself :S
Answers
Without the API key it's hard to test against your data source. Works fine with sample data:
That's true. I tried your example in the p5js editor and works no problems.
This is the code I run to test (with API key):
in which line do you get the error?
where do you use 'responseText' ?
what do you get here
println(data);
@GoToLoop: the error does look CORS related, but the whole point of jsonp is that it should work without CORS set on the data server. It looks like the data being returned isn't in the right format to me: jsonp should define a function that wraps the json but all I see is raw json. Which is why your workaround works...
See the Wikipedia entry:
@iamlukesky: check the docs for your data source: there may be an additional url_var required to get a jsonp formatted response, or if you're generating it with some server side code yourself read up on the jsonp format ;)