Search code examples
jsonangularjshttpebay-api

angularjs $http.get-Request for json on ebay doesn't work


I'm confused.. please help me
when I put an ebay-search-Request:

http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE[...]

in my browser, it shows a JSON. I copied the JSON in a file called v1 on my webspace:

http://bloxxer.net/autentik/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=blabla;D&GLOBAL-ID=EBAY-US&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&keywords=fahrrad&paginationInput.entriesPerPage=3&itemFilter(0).name=MaxPrice&itemFilter(0).value=25&itemFilter(0).paramName=Currency&itemFilter(0).paramValue=EUR&itemFilter(1).name=ListingType&itemFilter(1).value(0)=AuctionWithBIN&itemFilter(1).value(1)=FixedPrice

A $http.get-Request to my webspace gives me the JSON:

var AutApp = angular.module('AutApp', []);

AutApp.controller('ctrl', function($scope,$http) {

    $scope.search_ebay = function(keys) {
    //    var url = "http://svcs.ebay.com/services/search/FindingService/v1";
        var url = "http://bloxxer.net/autentik/v1";
            url += "?OPERATION-NAME=findItemsByKeywords";
            url += "&SERVICE-VERSION=1.0.0";
            url += "&SECURITY-APPNAME=ID"; // doesnt work on ebay without ID
            url += "&GLOBAL-ID=EBAY-US";
            url += "&RESPONSE-DATA-FORMAT=JSON";
            url += "&REST-PAYLOAD";
            url += "&keywords=fahrrad";// + keys;
            url += "&paginationInput.entriesPerPage=3";
        $http.get(url).
        success(function(data) {
            var items = data.findItemsByKeywordsResponse[0].searchResult[0].item || [];
            var html = [];
            $scope.ebay_suchergebnis='<table width="100%" border="0" cellspacing="0" cellpadding="3"><tbody>';
            for (var i = 0; i < items.length; ++i) {
                var item     = items[i];
                var title    = item.title;
                var pic      = item.galleryURL;
                var viewitem = item.viewItemURL;
                if (null != title && null != viewitem) {
                  $scope.ebay_suchergebnis+='<tr><td><a href="' + viewitem + '" target="_blank">' + '<img src="' + pic + '" border="0">' + title + '</a></td></tr>';
                }
            }
        })
        .
        error(function(data,status) {
            $scope.ebay_suchergebnis=data+"#"+status;
        });
    };
    $scope.sucheingabe="";
});

But if i change the $http.get-Request to svcs.ebay.com (with my ID), it gives me "null#0".
Has someone an idea? Thank you!


Solution

  • the solution is so easy.. nothing with cors and co.. just make a detour on your server:

    <?php
    $url = "http://svcs.ebay.com/services/search/FindingService/v1";
    $url .= "?OPERATION-NAME=findItemsByKeywords";
    $url .= "&SERVICE-VERSION=1.0.0";
    $url .= "&SECURITY-APPNAME=YOUR_ID_IN_EBAY";
    $url .= "&GLOBAL-ID=EBAY-US";
    $url .= "&RESPONSE-DATA-FORMAT=JSON";
    $url .= "&REST-PAYLOAD";
    $url .= "&".$_SERVER["QUERY_STRING"];
    
    
    $json = file_get_contents($url);
    echo $json;
    

    the javascript:

    AutApp.controller('ctrl', function($scope,$http) {
        $scope.search_ebay = function(keys,anzahl) {
    
            var url='http://YourDomain.com/json_abfragen.php?keywords='+keys+"&paginationInput.entriesPerPage="+anzahl;
    
            $http.get(url).
            success(function(data) {
                var items = data.findItemsByKeywordsResponse[0].searchResult[0].item || [];
                var ebay_suchergebnis='<table><tr>';
                for (var i = 0; i < items.length; ++i) {
                    var item = items[i];
                    if (null != item.title && null != item.viewItemURL) {
                      ebay_suchergebnis+='<td><a href="' + item.viewItemURL + '" target="_blank"><img src="'+item.galleryURL+'"><br>' + items[i].title + '</a></td>';
                    }
                }
                ebay_suchergebnis+='</tr></table>';
                document.getElementById('here').innerHTML=ebay_suchergebnis;
            })
            .
            error(function(data,status) {
                // errorprocessing
            });
        };
    });