Blockchain 使用Web3.js连接到本地Ganache实例时出错

Blockchain 使用Web3.js连接到本地Ganache实例时出错,blockchain,ethereum,web3js,Blockchain,Ethereum,Web3js,我有一个在本地Node.js服务器(端口3000)上运行的web应用程序和一个在端口7545上运行的Ganache实例。每当我尝试使用Web3.js连接到Ganache实例时,浏览器就会出现CORS错误 Node.js - v8.11.3 Ganache - v1.2.3 Web3 - web3.min.js (v0.20.7) OS - Windows 10 我尝试过安装禁用CORS的Chrome扩展,但没有效果 这是我用于创建和连接Web3对象的代码: var web3;

我有一个在本地Node.js服务器(端口3000)上运行的web应用程序和一个在端口7545上运行的Ganache实例。每当我尝试使用Web3.js连接到Ganache实例时,浏览器就会出现CORS错误

Node.js - v8.11.3
Ganache - v1.2.3
Web3 - web3.min.js (v0.20.7)
OS - Windows 10
我尝试过安装禁用CORS的Chrome扩展,但没有效果

这是我用于创建和连接Web3对象的代码:


    var web3;
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    }

这是每当我点击执行Web3和Ganache之间连接的按钮时,从Chrome上得到的错误消息:

Access to XMLHttpRequest at 'http://localhost:7545/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
inpage.js:1 Uncaught Error: CONNECTION ERROR: Couldn't connect to node http://localhost:7545.
    at Object.InvalidConnection (inpage.js:1)
    at o.send (inpage.js:1)
    at a.send (inpage.js:1)
    at w.accounts (inpage.js:1)
    at HTMLButtonElement.<anonymous> (main.js:250)
    at HTMLButtonElement.dispatch (jquery.min.js:3)
    at HTMLButtonElement.r.handle (jquery.min.js:3)
InvalidConnection @ inpage.js:1
jquery.min.js:4 POST http://localhost:3000/ 500 (Internal Server Error)
访问位于的XMLHttpRequest'http://localhost:7545/“起源”http://localhost:3000'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:当请求的凭据模式为'include'时,响应中'access control Allow Origin'标头的值不得为通配符'*'。XMLHttpRequest启动的请求的凭据模式由withCredentials属性控制。
inpage.js:1未捕获错误:连接错误:无法连接到节点http://localhost:7545.
在Object.InvalidConnection(inpage.js:1)
在o.send(inpage.js:1)
at a.send(inpage.js:1)
在w.accounts(inpage.js:1)
在HTMLButtoneElement。(main.js:250)
在HTMLButtonElement.dispatch(jquery.min.js:3)
位于HTMLButtonElement.r.handle(jquery.min.js:3)
InvalidConnection@inpage.js:1
jquery.min.js:4篇文章http://localhost:3000/ 500(内部服务器错误)
有什么想法吗?
非常感谢

在package.json文件中添加node.js服务器的代理

  "proxy": "http://localhost:5000"

感谢您的建议,但不幸的是,我一直收到相同的错误。如果您使用express server安装程序运行nodejs,那么您可以使用package cors来处理此错误。谢谢!我现在正在使用ExpressCors,现在它正在运行finecors是一个很棒的库,它可以通过ExpressFramework处理与cors相关的问题。您可以将您的项目上传到GitHub,以便我可以更好地帮助您吗?