1

I'm working on a javascript program that needs to get data from an API.

My problem is I'm not able to go from the url string to the actual JSON data.

I've looked here and they provide this answer :

Put your JSON string a variable var jsonString

then...

var myJsonObject = JSON.parse(jsonString);

So I ended up with :

var url = "https://pixabay.com/api/?";

var data = JSON.parse(url);

But this is not working. I'm sure I'm missing something obvious but I can't see what.

Any help would be greatly appreciated, thanks !

4
  • 1
    At the moment you're just trying to JSON parse a URL string, you need to make a call to retrieve the content at that URL, then parse the content. Commented Mar 27, 2016 at 16:14
  • api.jquery.com/jquery.getjson Commented Mar 27, 2016 at 16:15
  • Thanks a lot DBS, how do I make a call to retrieve the content ? That's where I'm stuck :/ Commented Mar 27, 2016 at 16:29
  • Alon Eltan I've looked into this documentation but I must admit this is a lot of complex information to me, most of the syntax in this page is new to me ( I don't know anything about ajax or jQuery ). Can someone try and explain to me with maybe a more specific answer ? Commented Mar 27, 2016 at 16:35

1 Answer 1

0

You need to pass variables and their values in the url.

var url = "https://pixabay.com/api/?var1= " + var1;
var var1 = req.params.var1;
var jsonString = { "var1": var1 }
var data = JSON.parse(url);

You need the string that is in JSON format before you can parse it.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand. The url is completed with fixed values ( search parameters like API key, key words, image type and so on ). What I need is somehow to get the script to fetch the data at that url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.