Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <script src="myJs.js"></script>
- </head>
- <body>
- <div id="res" dir="rtl">Loading data</div>
- <center><input type="button" value="click me" onclick="runMe()"/></center>
- </body>
- </html>
- var myURL = "https://newsapi.org/v2/top-headlines?country=il&apiKey=f6619618319e4467ac2929cdba33833a"
- //fetch - es6
- function runMe(){
- fetch(myURL)
- .then (function (response){
- if (response.status != 200){
- console.log("error code:"+response.status);
- return;
- }
- //examine the text in the response
- response.json().then(function(data){
- console.log(data.articles);
- var myData = data.articles;
- var displayData=""; //"<center>";
- for (var counter=0;counter<myData.length;counter+=1){
- displayData+="<b><u>"+myData[counter].title+"</b></u><br/>";
- displayData+=myData[counter].description+"<hr/>";
- }
- //displayData+="</center>";
- res.innerHTML=displayData;
- });
- }
- )
- .catch(function(err){
- console.log("Huston, we have a problem:");
- console.log(err);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement