API ile çekilen haberlere kaynak eklemek

Bir web tasarım ödevim var ve api çekmemiz isteniyor. nasıl çektiğini benimle paylaşır mısın*

Browser ile yapıcaksan bu işlemi fetch API nedir araştırabilirsin, işin kompleks değilse çok basit bir işlem:

JavaScript:
// Buraya API isteği atıcağın server'ın adresini giriyorsun.
const fetchDomain = "https://example.com/"

// Bu server'dan dönen data'yı console'a basıcak.
fetch(fetchDomain)
  .then(response => response.json())
  .then(json => console.log(json))

// Bu fonksiyon da data'yı döner.
async function fetchData(){
    const response = await fetch(fetchDomain)
    return response.json();
}
 
Browser ile yapıcaksan bu işlemi fetch API nedir araştırabilirsin, işin kompleks değilse çok basit bir işlem:

JavaScript:
// Buraya API isteği atıcağın server'ın adresini giriyorsun.
const fetchDomain = "https://example.com/"

// Bu server'dan dönen data'yı console'a basıcak.
fetch(fetchDomain)
  .then(response => response.json())
  .then(json => console.log(json))

// Bu fonksiyon da data'yı döner.
async function fetchData(){
    const response = await fetch(fetchDomain)
    return response.json();
}
JavaScript:
var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://api.collectapi.com/imdb/imdbSearchById?movieId=tt1375666");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("authorization", "apikey your token");

xhr.send(data);

collectapiye üye oldum. Mesela böyle bir şey var. imdbden veri çekmek için. Fakat bunu nasıl kullanacağımı bilemedim. bu konuda yardımcı olabilir misin?
 
JavaScript:
var data = null;

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "https://api.collectapi.com/imdb/imdbSearchById?movieId=tt1375666");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("authorization", "apikey your token");

xhr.send(data);

collectapiye üye oldum. Mesela böyle bir şey var. imdbden veri çekmek için. Fakat bunu nasıl kullanacağımı bilemedim. bu konuda yardımcı olabilir misin?

Bahsettiğim Fetch API, XML request'in modern hali, bu kadar koda hiç gerek yok.

Şu da aynı işi görücektir:

JavaScript:
const fetchUrl = 'https://api.collectapi.com/imdb/imdbSearchById?movieId=tt1375666'

// Gelen veri bir Javascript Object şeklinde browser console'unda yazılıcak.
fetch(fetchUrl)
  .then(response => response.json())
  .then(json => console.log(json))
 
Bahsettiğim Fetch API, XML request'in modern hali, bu kadar koda hiç gerek yok.

Şu da aynı işi görücektir:

JavaScript:
const fetchUrl = 'https://api.collectapi.com/imdb/imdbSearchById?movieId=tt1375666'

// Gelen veri bir Javascript Object şeklinde browser console'unda yazılıcak.
fetch(fetchUrl)
  .then(response => response.json())
  .then(json => console.log(json))
Buradan bu api yi çekebilmek için api keyimi kullanmam gerekiyor. onu nasıl kullanıcam bu yöntemde. Yani ben daha birinci sınıfım. Çok detaylı bilmiyorum. Sadece proje ödevinde koymamız istenmiş.
 
Uyarı! Bu konu 5 yıl önce açıldı.
Muhtemelen daha fazla tartışma gerekli değildir ki bu durumda yeni bir konu başlatmayı öneririz. Eğer yine de cevabınızın gerekli olduğunu düşünüyorsanız buna rağmen cevap verebilirsiniz.

Technopat Haberler

Yeni konular

Yeni mesajlar

Geri
Yukarı