Ask coding questions

← Back to all posts
Jsonstore.io in HTML
ryanhcode (90)

I have used jsonstore.io before, and am now trying to integrate it into a web app. I can't use require, because I am running it inside of a script tag in HTML. How would I do this?

Answered by JustARatherRidi (191) [earned 5 cycles]
View Answer
Commentshotnewtop
JustARatherRidi (191)

Talking to jsonstore involves sending them an HTTP request. In fact, connecting to any website basically is you sending them an HTTP request and them sending you a response. So you can imagine why it is a feature that most languages support.

In browser javascript, using an XmlHttpRequest like @leon332157 suggested is possible, but it gets quite tedious. So people would usually use a library such as jQuery to abstract away all of it.

How you should be doing it today is using the Fetch API.

Basically, it lets you do this:

const result = await fetch(urlOfYourJsonstoreEndpoint);
const data = await result.json();

// All your data is now in the 'data' variable

Which is about as easy as it gets, really.


Here are some useful links


If you encounter any problems in using it, I'll be more than happy to help!

ryanhcode (90)

Thanks! I’m now comfortable with XmlHttpRequests so I’ll be using those. @JustARatherRidi

JustARatherRidi (191)

@ryanhallihan No problem! ^^

It's nice to see that you've solved your problem, but I think you might want to seriously consider switching to fetch, it really is a lot easier. I might be able to convince you as to how much easier it is, could you share your code?

ryanhcode (90)

@JustARatherRidi I actually ended up sending a request to the server and used an npm package for jsonstore that makes it really easy. I don’t think I need to use fetch.

JustARatherRidi (191)

@ryanhallihan oh okay, it seemed like you had to do it from within a <script> tag from your question. If you're able to use node to host your website, then using an npm package is probably the best way to go.

Cheers!

leon332157 (69)

You can use XmlHttpRequest to make requests to jsonstore in browser JS.

ryanhcode (90)

@leon332157 Thanks! I've never used that before so if anyone could give me pointers on that as well it would be nice.

heyitsmarcus (287)

@ryanhallihan The only problem you'll run into with jsonstore is CORS when running requests client side. You can initiate GET requests just fine, but you won't be able to POST, PUT, or PATCH any data back to the server without a server side implementation such as NodeJS.

Check out their GitHub page (in the JavaScript section) where there are sample requests:

https://github.com/bluzi/jsonstore#javascript