Expo React Native encountering a network request failure while trying to fetch API and load a local JSON

I am attempting to load a static json file from a local source in my Expo managed application using the following code snippet.

useEffect(()=>{

getData()},['']);

function getData(){

fetch('../Audio/AudiosObj.json',{'Content-Type':'application/json','Accept': application/json'}).then((data)=>(console.log(data)).catch((err)=>(console.log(err)) } 

Upon running the application, I encountered an error message stating "Network request failed". Can you provide any insights into what may be causing this issue?

Answer №1

Instead of using fetch to retrieve JSON data, you can simply import the JSON file directly with an import statement.

import AudiosObjJSON from '../Audio/AudiosObj.json';

If you do choose to use fetch, make sure to provide the proper headers like this:

fetch('../Audio/AudiosObj.json',{
   headers: { 
     'Content-Type': 'application/json',
     'Accept': 'application/json'
   }
})
.then(...)
.catch(...);

However, I recommend utilizing the import statement as it is more efficient and easier to read compared to using fetch.

Additionally, the fetch method may not work correctly after compiling the app and including all assets due to potential issues with relative paths.

On the other hand, when using the import statement, the file will be integrated into the bundle itself and should not cause any problems post-compilation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Divide the division inside the box by clicking on it

I am looking to create a unique div with a random background color and a width of 100px. Additionally, I want to have a button that, when clicked, will split the original div into two equal parts, each with its own random background color. With each subs ...

Error when saving data in database (MongoDB) due to a bug

When setting up a database schema, sometimes an error occurs in the console indicating that something was not written correctly. How can this be resolved? Here is the code: let mongoose = require(`mongoose`); let Schema = mongoose.Schema; let newOrder = ...

Variable in Javascript file causing return value to be 'undefined'

I have encountered an issue with my JavaScript file. It is extracting data from a SharePoint list and displaying it on an HTML page, but one of the fields appears as 'undefined' even though I defined it initially. The problematic variable is &ap ...

Generatively generating a new element for the react application to be mounted on

I am looking to enhance my JavaScript application using React by creating a build. Currently, the application consists of just a single file structured as follows. This file essentially creates a div element and continuously changes the color of the text & ...

Retrieve a dynamic HTML object ID using jQuery within Angular

In my Angular application, I have implemented three accordions on a single page. Each accordion loads a component view containing a dynamically generated table. As a result, there are three tables displayed on the page - one for each accordion section. Abo ...

Unable to retrieve object using ajax GET from the controller

I'm facing an issue where I am trying to send back an object in JSON format to my JavaScript code. However, when attempting to do so, the error event is being triggered with a parseerror message. What could be causing this problem? $.ajax({ url: ...

Spin the connections around a circular path

I'm interested in creating a website where links rotate around a circle, similar to this example: https://i.sstatic.net/103mx.jpg. The links will display different images and texts leading to various URLs. I want the images to form a unified rotation ...

The jQuery autocomplete feature presents all choices regardless of what is typed into the input field

I'm currently working on a jQuery script that retrieves a JSON response and creates individual "player" objects based on the data received. These player objects are then added to the availablePlayers array, which serves as the data source for the aut ...

Using AJAX to fetch data for Highcharts illustration

Hello there! I am attempting to create a chart based on data retrieved via AJAX. The values for the chart are coming from the AJAX return. Here is the data that I have: "tgl":["2016-07-12 00:00:00.000","2016-07-01 00:00:00.000"],"total":[1283947,12346185 ...

Troubleshooting the Ng2-Charts Linechart to display all values instead of just the first two

Starting a new Angular application with the Angular-CLI (beta 22.1) has presented an issue when adding test data (5 values) to a chart. The scaling appears incorrect, displaying only the first two values stretched across the entire length of the graph (ref ...

Can we effectively manage the input for a component that is created dynamically in real-time?

Please note: I am a newcomer to ReactJS, JavaScript, and the world of front-end development in general. Hello everyone, I have made the decision to create a versatile component that can handle all the forms within my project based on a predefined templat ...

Is it considered acceptable to utilize a v-model's value as the basis for an if-statement?

How can I incorporate the v-model="item.checked" as a condition within the validations() function below? <table> <tr v-for="(item, i) of $v.timesheet.items.$each.$iter" > <div> <td> ...

Error: Unable to call topFrame.window.changeSelectedBarStyle because it is not a valid function. What could be causing this

This function is called changeSelectedBarStyle: function changeSelectedBarStyle(tdId){ $("#menuTable td").each(function(index){ if(this.id == tdId){ $(this).removeClass("menuPanel"); $(this).addClass("menuPanelSelected" ...

What is the best way to modify a state in nextjs?

Recently, I've been working on a next.js project that includes a shopping cart feature. Essentially, when you click on an item, its image is added to a list and displayed with all the other selected items. To remove an item, users can simply click on ...

What is the syntax for implementing a nested for loop in JavaScript within this particular scenario?

var entries = [ { "item": "something", "steps": [{ "node": {"name": "test0"}, "status": {"name": "test"}, "time": {"name": "test"} },{ "node": {"name": "test1"}, ...

React Native - Troubleshooting SQLite setup glitch

Initially, I attempted to search for questions that were similar to the issue I am facing, but unfortunately came up empty-handed! If this question has already been answered previously, please direct me to it and I apologize for any inconvenience. Transit ...

What could be causing the delay in my scroll event with React Three Fiber?

While using React Three Fiber, I encountered an issue with a react component that generates a sprite which is supposed to remain constant in size regardless of camera zoom. Although the algorithm appears to be functioning correctly (the size does not chang ...

Tips for altering the class of an HTML button dynamically when it's clicked during runtime

I currently have a set of buttons in my HTML page: <button id="button1" onclick="myFunction()" class="buttonClassA"></button> <button id="button2" onclick="myFunction()" class="buttonClassA"></button> <button id="button3" onclic ...

I am looking to modify the dimensions of the grouped GridHelper through the graphical user interface

I'm having trouble resizing the grid using the GUI interface. How can I adjust its size? Here are the steps I followed to create it. let scene = new THREE.Scene(); scene.background = new THREE.Color(0x222222); let group = new THREE.Group(); scene.add ...

Is there a way to apply toggling and styles to only the card that was clicked in Vue.js?

DisplayBooks.vue consists of a single page responsible for showcasing books fetched from a backend API. Each card on the UI features two buttons - ADD TO BAG and ADDED TO BAG. When a user clicks on the ADD TO BAG button of a specific card, it should toggle ...