0

I am making a personal NEWS website. In my website I have made different sections like Tech,Sports,Business,General News. I am making different requests to different NEWS API using getJSON() in jquery.

Here is my code in my code you can see only source=" " is changing:

$.getJSON('https://newsapi.org/v1/articles?source='some-source'&sortBy=top&apiKey=myapi-key',function(json) {
        console.log(json);
    });

  $.getJSON('https://newsapi.org/v1/articles?source='some-source'&sortBy=top&apiKey=myapi-key',function(json) {
            console.log(json);
        });

  $.getJSON('https://newsapi.org/v1/articles?source='some-source'&sortBy=top&apiKey=myapi-key',function(json) {
                console.log(json);
            });

Each JSON data gives 4 to 5 objects(see screenshot for clarification) but that is not sufficient for my website so I am making multiple requests to API so that I can display many articles. Can you please suggest me is this right way to do as I have seen many forums and tutorials, they say repeating your code is bad practice.

JSON response:

enter image description here

2
  • You put the URLs into an array, iterate over it, and get the news. Commented Jun 18, 2017 at 13:48
  • @AdamAzad Thanks I got it. Commented Jun 19, 2017 at 3:17

1 Answer 1

1

You can make a function and pass the different API sources to the function as shown below, thus not rewriting your code. Hopefully this helps

function getNewsFeed(var url) {
    $.getJSON(url, function(json) {
        console.log(json);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.