Access to fetch blocked has been blocked by CORS p

2019-07-26 18:03发布

I'm trying to do a get request of a server but keep getting a CORS error, I'm not terribly experienced with https requests but this is what my error looks like:

binSize=1h&symbol=XBT&count=10&startTime=2019-01-26&endTime=2019-01-27 403

Access to fetch at 'https://www.bitmex.com/api/v1/trade/bucketed?binSize=1h&symbol=XBT&count=10&startTime=2019-01-26&endTime=2019-01-27' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Uncaught (in promise) TypeError: Failed to fetch

I have tried the code below but I'm not too sure how to modify it and there's not a lot of documentation on react and this specific type of call, I'm not married to this method it just seemed like the better option any method that gets me the data will suffice.

This is what I currently have:

var https = require('https');

//contains execution environment and price history

module.exports = {

//gets pricing data for a given asset on bitmex within a set time frame.
fetchPricingBucket: function(timeFrame, symbol, windowSize, startTime, endTime, callback) {

    //append arguments to request
    var request = "https://www.bitmex.com/api/v1/trade/bucketed?";
    request += "binSize=" + timeFrame + "&";
    request += "symbol=" + symbol + "&";
    request += "count=" + windowSize + "&";
    request += "startTime=" + startTime + "&";
    request += "endTime=" + endTime;

    https.get(request, function (res) {
        const {
            statusCode
        } = res;
        const contentType = res.headers['content-type'];

        //check for erroers
        let error;
        if (statusCode !== 200) {
            error = new Error('Request Failed.\n' +
                `Status Code: ${statusCode}`);
        }
        else if (!/^application\/json/.test(contentType)) {
            error = new Error('Invalid content-type.\n' + `Expected application/json but received ${contentType}`);
        }
        if (error) {
            console.error(error.message);
            // consume response data to free up memory
            res.resume();
            return;
        }

        // Buffer the body entirely for processing as a whole.
        var bodyChunks = [];
        res.on('data', function(chunk) {
            // You can process streamed parts here...
            bodyChunks.push(chunk);
        }).on('end', function() {
            callback(JSON.parse(bodyChunks), error);
            // ...and/or process the entire body here.
        })
        //returns the json response and error to the calling function.
    });
},

}

No Luck so far but accepting suggestions!

0条回答
登录 后发表回答