web3.js请求提供者的跨域解决方案

时间:2019-01-09 07:11:03

标签: cross-domain solution

web3.js请求提供者的跨域解决方案,这个问题与道德有关。

我使用nginx反向代理来处理跨域问题, 但是web3.js request https://xxx.xxx/5b44529e903c4ebf9d60e4d01c965489抛出

Uncaught Error: Can't autodetect provider for "xxx.xxx/5b44529e903c4ebf9d60e4d01c965489""

nginx的跨域配置如下

================================ nginx开始============== ==

location /5b44529e903c4ebf9d60e4d01c965489{

if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain; charset=utf-8';
    add_header 'Content-Length' 0;
    return 204;
 }
 if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
 }
 if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
    add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
 }

proxy_pass https://mainnet.infura.io/v3/5b44529e903c4ebf9d60e4d01c965489;
}

===================== nginx end ===============

web3.js代码

=================== web3.js start ============

import data from './abi'
import Web3 from 'web3';
export default {
    install: function (Vue, options) {
         var web3 = window.web3;
        var contract = '';
        //web3 = new Web3("https://mainnet.infura.io  /v3/5b44529e903c4ebf9d60e4d01c965489");
        web3 = new Web3("www.xxx.xxx/5b44529e903c4ebf9d60e4d01c965489");
        contract = new web3.eth.Contract(data.abi, data.address);
        Vue.prototype.infuraContract = contract;
        Vue.prototype.infuraWeb3 = web3;
        //Web3 version "web3": "^1.0.0-beta.36";
    }
}

=================== web3.js结束==================

0 个答案:

没有答案