reactjs - React'te public klasöründen yerel JSON dosyası alınamadı
İyi Günler,
React uygulamamın public klasöründe bulunan yerel JSON dosyamdan veri çekmeye çalışıyorum. Bunun SO'da sıkça sorulan bir soru olduğunu biliyorum ve önceden konuyla ilgili başlıkları inceledim ama aşağıdaki en yakın olanı.
ReactJS ile genel klasörden yerel JSON dosyasını al
Json dosyamı almak için aşağıdaki kodu kullanıyorum:
const getResponse = () => {
fetch('responseScript.json')
.then(response => { response.json() })
.then(data => { console.log(data) })
}
useEffect(() => {
getResponse();
})
Ana sorun, json dosyamdan veri çektiğimde undefined ve bir hata almamdır:
responseScript, public klasöründe. Ayrıca devtools'u da kontrol ettim ve network sekmesinde, isteğin durum kodunun 200 OK olduğunu görebiliyorum.
Soruna katkıda bulunup bulunmadığından emin değilim ama TypeScript kullanıyorum. Ayrıca FileStructure'ı kullanmayı da denedim ve bu da benim için işe yaramıyor gibi görünüyor.
DÜZENLE
Benim JSON yapım şöyle görünüyor:
[
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
},
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
},
{
"Category":"Some text here",
"Issue":"Some text here",
"Answer":"Some text here",
"Response":"Some text here"
}
]
Klasör yapısı şudur
Her türlü yardım takdir edilir. Teşekkürler!
Cevap #1
bunu kullan:
const getResponse = () => {
fetch("./public/responseScript.json")
.then(response => { response.json() })
.then(data => { console.log(data) })
}

