UNSOLVED What's the best way to fetch JSON data?
I’m hoping some of your developer knowledge can help me out here.
What is the best/recommended way to retrieve JSON (or other) data from external APIs? Up to now I’ve been using
fetch()in the main module .js file, but after hitting some CORS snags, I’m wondering if there is a better method?
@creepinjesus cors has nothing to do w fetch. request and axios will have the same problems.
thats the server side protecting itself
i generally dont use the builtin .json() methods provided by fetch and others, cause i dont like the error handling of the actual data transfer part.
i use JSON.parse() with try/catch around it
or JSON.stringify() to get the json text
@creepinjesus cors has nothing to do w fetch
No, that’s fair enough. Just checking it’s not the fetch method itself.
Is it possible then to even access JSON that’s being blocked by CORS using some other method? Would making the request from node_helper make a difference?
The data is being served on a local server and I can use curl or a browser to access it without issue; just not from a script in the browser (of course).
@creepinjesus is the address: in config.js “localhost”?
try changing to “0.0.0.0”
or vice versa
That didn’t help, but I’ve fixed the CORS issue by allowing requests from that origin on the server in question anyway.
Kind of digressing from the original question but if fetch does the job then I suppose it’s good enough!
@creepinjesus when the server is remote there are urls u can use to proxy your real url
search for cors proxy