简体   繁体   English

来自solidity合约的app.js文件中的getJSON()出错

[英]Getting error in getJSON() in app.js file from solidity contract

I am making a simple dapp in truffle.我正在用松露制作一个简单的 dapp。 When I use $.getJSON(), it throws an error.当我使用 $.getJSON() 时,它会引发错误。

My app.js:我的 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.provi/ders.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();
    });
},

This is not complete app.js code.这不是完整的 app.js 代码。 Only the main part.只有主要部分。

Solidity File: FruitBasket.sol (contract name: FruitBasket) Solidity 文件:FruitBasket.sol(合约名称:FruitBasket)

I have included every script in index.html.我已将每个脚本都包含在 index.html 中。 When I run this project using npm run dev , I get these in the terminal.当我使用npm run dev运行这个项目时,我在终端中得到了这些。

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

I am getting 404 in FruitBasket.json我在 FruitBasket.json 中得到 404

Also, in browser console, error displaying此外,在浏览器控制台中,错误显示

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

I am totally new in this, so please tell me if I need to provide more information or any other code/file.我对此完全陌生,所以请告诉我是否需要提供更多信息或任何其他代码/文件。

Try this:尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我定义了组件并使用它 App.js 但出现未定义的错误 - I defined component and used it App.js but getting undefined error 出现错误:没有要呈现的“App.js”的默认导出 - Getting error: No default export of 'App.js' to render 在 app.js 中导入 js 文件时出现未知文件扩展名错误,使用类型:package.json 中的模块 - I am getting unknown file extension error while importing a js file in app.js, using type: module in package.json 尝试将控制器与angularjs一起使用,从app.js提取控制器代码并将其放入控制器文件时出错 - Attempting to use controllers with angularjs, error when taking controller code from app.js and putting into a controller file 我无法破解的app.js文件错误 - Error in app.js file that I can't crack 从资源文件夹添加app.js文件不公开 - Adding a app.js file from resource folder not public 如何在 React 中将状态从组件传递到文件 app.js - How to pass state from component to file app.js in React 需要来自 ejs 文件的 app.js 中 html 标记的 ID - Require Id of html tag in app.js from ejs file React-从根app.js文件导出无法正常工作 - React - exports from root app.js file not working 包括从npm下载到app.js的javascript文件 - Include javascript file downloaded from npm to app.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM