Search code examples
javascriptcorsebay-api

eBay API CORS request using JavaScript returns undefined


I want to fetch JSON data from eBay Sandbox using JavaScript

I tried getting it through CORS request (as it is a cross domain request) but it returns undefined. I tried lots of different code but I haven't found any solutions. What I want to do is that fetch the products from eBay and display them in my Chrome extension.

Any help is appreciated.


Solution

  • I found a solution by making a CORS request and using the CORS Anywhere API from https://github.com/Rob--W/cors-anywhere/

    var cors_api_url = 'https://cors-anywhere.herokuapp.com/';
    function doCORSRequest(options, printResult) {
        var x = new XMLHttpRequest();
        x.open(options.method, cors_api_url + options.url);
        x.onload = x.onerror = function() {
            printResult(
                options.method + ' ' + options.url + '\n' +
                x.status + ' ' + x.statusText + '\n\n' +
                (x.responseText || '')
            );
        };
        x.send(options.data);
    }
    (function() {
        var outputField = document.getElementById('output');
        new1();
        function new1() {
            // e.preventDefault();
            doCORSRequest({
                method: 'GET',
                url: url,
            }, function printResult(result) {
                //result contains the response
                //write your code here
            });
        };
    })();
    

    Source: https://github.com/Rob--W/cors-anywhere/blob/master/demo.html

    (Live example: https://robwu.nl/cors-anywhere.html)