Encountering a problem with the getJSON() function within the app.js file connected to a solidity

Currently, I am working on developing a simple dapp in Truffle. However, I encountered an error when using $.getJSON() function in my app.js file.

Below is a snippet of my app.js:

App ={

web3Provider: null,
contracts: {},

init: function (){
    return App.initWeb3();
},

initWeb3: function (){
    if (typeof web3 != "undefined"){
        App.web3Provider = web3.currentProvider;
        web3 = new Web3(web3.currentProvider);
        App.setStatus("Metamask Detected!");

    }
    else {
        alert("Error. Try Again Later");
        //App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
        web3 = new Web3(App.web3Provider);
        return null;
    }

        account = ethereum.request({ method: 'eth_accounts' });

        if (!account){
            //alert("Cannot Fetch Account. Make sure you are logged in!");
            App.setStatus("Please login to MetaMask");
            return ;
        }
        else {
            console.log('Account detected');
            console.log(account)
        }
        return App.initContract();

    },

initContract: function (){

    $.getJSON("FruitBasket.json", function (FruitBasketArtifact){
        
        App.contracts.FruitBasket = TruffleContract(FruitBasketArtifact);
        App.contracts.FruitBasket.setProvider(App.web3Provider);
        //return App.getContractProperties();
    });
},

The above code represents the main part of the app.js file and not its entirety.

Solidity File: FruitBasket.sol (contract name: FruitBasket)

All scripts have been included in index.html file. Upon running the project using npm run dev, I encounter the following logs in the terminal.

21.05.06 18:22:34 304 GET /index.html
21.05.06 18:22:34 304 GET /js/bootstrap.min.js
21.05.06 18:22:34 304 GET /js/web3.min.js
21.05.06 18:22:34 304 GET /js/truffle-contract.js
21.05.06 18:22:34 304 GET /js/app.js
21.05.06 18:22:34 404 GET /FruitBasket.json

The issue arises with a 404 error for FruitBasket.json file.

In addition, the browser console displays an error as shown below:

GET http://localhost:3002/FruitBasket.json

I am relatively new to this, so feel free to ask if more information or other code/files are needed.

Answer №1

Here's a suggestion for you to try:

 $.getJSON("./FruitBasket.json", function (FruitBasketArtifact){
   App.contracts.FruitBasket = TruffleContract(FruitBasketArtifact);
   App.contracts.FruitBasket.setProvider(App.web3Provider);
   // Call a function here
});

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

"Encountering a Type Error while attempting to destructure elements within ReactJS

Issue Upon querying objects from the GraphQl server and logging data, I can see the objects in the console. However, when attempting to destructure it as data: { getPosts : posts }, a type error is returned. Furthermore, trying to use map directly on data ...

What is the best way to deselect the first radio button while selecting the second one, and vice versa, when there are two separate radio groups?

I am looking to achieve a functionality where if the first radio button is selected, I should receive the value true and the second radio button should be unselected with the value false. Similarly, if the second radio button is selected, I should receive ...

Is there a way to restrict the number of line breaks in a multiline asp:TextBox control?

Is it possible to restrict a multiline asp:TextBox to only display content on 3 lines using either javascript or C#? ...

if and elsif JSON with Angular

Currently, I am working on completing a task within our project. To provide more context, I have a field with items that I must select. Once I choose an item, another field appears in which I must select additional elements. These elements need to be sen ...

Upgrading from Angular 5.2 to 6.0: Facing an issue where angular.json does not replace angular-cli.json

After diligently following the official documentation to upgrade from Angular 5.2 to Angular 6.0 (with the end goal of migrating to Angular 13), I found myself at a standstill. Executing the command NG_DISABLE_VERSION_CHECK=1 npx @angular/cli@6 update @an ...

Trouble with Bootstrap v4 toggle drop-down menu functionality detected in local environment, yet functions correctly when live on the

Today, I've encountered an issue with my Bootstrap v4 collapsible hamburger menu on my local XAMPP server. Interestingly, the menu works perfectly fine on my public website when the display is 768px wide in Chrome and Firefox. I have searched through ...

The image tag fails to appear on the browser when the client attempts to access it

Greetings, I am new to web development. I am attempting to create a simple static website that only displays an image in the header tag of an HTML file. The server seems to be working correctly in sending responses to the client, but the problem lies in t ...

How can one transfer information from a client to a server and complete a form using JavaScript?

Can the information be transferred from a client to the server using just JS, then fill out a form on the server, and finally redirect the client to view a pre-filled form? I am considering utilizing AJAX to send a JSON object, but unsure if it will be s ...

Adjust the tabs to fit perfectly within the container

I am currently facing an issue with my navigation bar. I have placed the <nav> tags within a container set to 100% width. However, when I adjust the height of the container by a certain percentage, the <ul> & <li> elements in the nav ...

Failed to perform the action using the Angular Post method

Currently, I am exploring the use of Angular with Struts, and I have limited experience with Angular. In my controller (Controller.js), I am utilizing a post method to invoke the action class (CartAction). Despite not encountering any errors while trigge ...

What is the best way to call a JavaScript function within a PHP echo statement that outputs a <div> element?

Having trouble echoing this PHP code due to issues with single quotes, causing the HTML to end prematurely. Any suggestions on how to fix this? function button($conn){ $sql = "SELECT * FROM table"; $result= mysqli_query($conn, $sql); while($r ...

How to fill an HTML template using ngFor without using a JSON array

Is there a way to fill the HTML with basic JSON like below: { "id": 34, "name": "Tip 33", "description": "Tip description 33", "created_at": "2018-01-26 18:59:19", "updated_at": "2018-01-26 18:59:19" } The code i tried using was: < ...

The JavaScript function for converting a date to a local string in the format of DD MMM YYYY is causing an error message in the browser console stating that it is not a valid function

I am encountering an issue with formatting a date string. The date is currently in the format 2021-03-31T00:00:00, and I need it to be displayed as 31 Mar 2021. In my TypeScript code, I attempted to use the following function: const formattedDate = i.Susp ...

Dividing CSV Data into Two File Outputs

I've got a CSV file that's structured like this: Docushare Host locale, created_at, version en,Wed Feb 21 17:25:36 UTC 2018,07.00.00.C1.609 User handle, client_data, docCountQuota User-12,,-2 Document handle,client_data,cfSpecialWords Document ...

How many intricate objects are instantiated in memory when running the code snippet provided above?

When looking at the given code, how many complex objects can we identify in memory? function Foo() { this.one = function() { return "one"; } } var f = new Foo(); Foo.two = function() { return "two"; }; Here's what I see: The Foo co ...

Unable to find 'react/lib/merge' module

I'm currently working on a React project and utilizing Babel and Webpack. Within one of my files, I have this require statement: var merge = require('react/lib/merge'); Unfortunately, I'm encountering the following error: ERROR in . ...

Filling an HTML modal with either keys or values from a JSON array

I'm facing a simple issue that I can't seem to solve. What am I missing here? I receive a PHP array through JSON encoding: var curr_op = <?php echo json_encode($options) ?>; When I console.log(curr_op), it shows: [14:35:00.889] ({' ...

I encountered an issue where the data I passed to a component ended up being undefined

So here's the scenario: I'm working on a Next.js project where I have a context for modals. In this context, I store modal details in an array called modalBase. Additionally, I fetch data from another context (toolsContext) to pass it to componen ...

JavaScript code may fail to load on initial page load

My JavaScript function utilizes ajax and is triggered when a button is clicked. Oddly, the function works perfectly if you visit the page for a second time, but fails to work on the first visit. I've attempted to include window.onload, document.onlo ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...