Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let myUrl="https://newsapi.org/v2/top-headlines?country=il&apikey=f6619618319e4467ac2929cdba33833a"
- //xmlHttpRequest - old school
- function runMe(){
- var myReq = new XMLHttpRequest();
- myReq.onload=reqListener;
- myReq.onerror=reqError;
- myReq.open('get',myUrl,true); //(method,url,async)
- myReq.send();
- }
- //listener
- function reqListener(){
- //parse our data into a json type
- var data = JSON.parse(this.responseText);
- res.innerHTML=data.articles[0].title;
- console.log(data.articles[0].title);
- }
- function reqError(err){
- console.log("Huston we have a problem:");
- console.log(err);
- }
- //fetch - es6
- runMeEs6();
- function runMeEs6(){
- 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[0].title);
- res.innerHTML=data.articles[0].title;
- })
- })
- .catch(function(err){
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement