繁体   English   中英

使用JavaScript的eBay API CORS请求返回未定义

[英]eBay API CORS request using JavaScript returns undefined

我想使用JavaScript从eBay沙盒中获取JSON数据

我尝试通过CORS请求(因为它是跨域请求)来获取它,但它返回的是未定义的。 我尝试了许多不同的代码,但没有找到任何解决方案。 我要做的是从eBay上获取产品,并在我的Chrome扩展程序中显示它们。

任何帮助表示赞赏。

您可以将GET请求发送到URL。

const http = require('http');

let url = 'http://svcs.sandbox.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=NiraliAc-FlashSal-SBX-7d56b4536-d82a9262&GLOBAL-ID=EBAY-US&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&keywords=harry%20potter&paginationInput.entriesPerPage=3&itemFilter(0).name=MaxPrice&itemFilter(0).value=25&itemFilter(0).paramName=Currency&itemFilter(0).paramValue=USD&itemFilter(1).name=FreeShippingOnly&itemFilter(1).value=true&itemFilter(2).name=ListingType&itemFilter(2).value(0)=AuctionWithBIN&itemFilter(2).value(1)=FixedPrice&itemFilter(2).value(2)=StoreInventory';

http.get(url, res => {
    let body = '';
    res.on('data', data => body += data);
    res.on('end', () => {
        console.log(body);
    });
});

我通过发出CORS请求并使用https://github.com/Rob--W/cors-anywhere/中的CORS Anywhere API找到了解决方案

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
        });
    };
})();

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

(实时示例: https : //robwu.nl/cors-anywhere.html

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM