Jsonstore.io in HTML
ryanhcode (91)

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?

You are viewing a single comment. View All
JustARatherRidi (194)

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 (91)

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

JustARatherRidi (194)

@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 (91)

@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 (194)

@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.