Implement Fetch API

Hello,
I have a data.json in script folder and it contains:

{
“test”: 10
}

in same folder there is game.js where I have used fetch:

fetch(“resources/data.json”)
.then(response => response.json())
.then(data => { console.log(data) })

it doesn’t work.
Is there any better way to use fetch or access .json file?.

Hi, you can put json file to resources folder, and load it.

before 2.4 version

// assets/resources/test.json
cc.loader.loadRes("test", (err, text:cc.JsonAsset) =>{
  debugger
  console.log(text.json);
})

after 2.4 version

// assets/resources/test.json
cc.assetManager.resources.load("test", (err, text:cc.JsonAsset) =>{
  debugger
  console.log(text.json);
})
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.