javascript - Angular HTTP GET request returns undefined while working in browser -
i'am learning angularjs , i've tried write basic script sending http request ebay public api, i've signed , got api keys, i've read docs several times , wrote basic code :
$scope.getqueryurl = function () { // unrelated code ... $scope.queryurl["ebay"] = "http://svcs.sandbox.ebay.com/services/search/findingservice/v1?operation-name=finditemsbykeywords&service-name=findingservice&service-version=1.0.0&global-id=ebay-us&security-appname="+dataauth.ebaykeyapi+"&response-data-format=xml&keywords="+$scope.qtext ; }; $scope.sendrequest = function () { $scope.getqueryurl(); // gets query url after adding parameters alert($scope.queryurl.ebay); $http.get($scope.queryurl["ebay"]).then( function(response){ alert("success" + response.data ); }, function(response){ alert("error" + response.statuscode ); }); };
how code should work :
it should create formated ebay query url, send through http request , sending response .
note : $scope.qtext
& dataauth.ebaykeyapi
assigned respective values .
what's problem:
the problem using angularjs script, code doesn't work, alert "error" shown, , response.statuscode
undefined .
but when copy formatted ebay query link in firefox works , xml response shown .
the formatted ebay query generated using script provided .
i think it's header related problem .
$http has default headers defined. $http sends json payload , accepts json response default. since dealing xml have explicitly specify accepted response type xml using header: accept: application/xml
please use following function appropriate headers , should response. also, please cross origin resource sharing (cors) restrictions on ebay api.
function getrequest(url) { $http({ method: "get", url: url, headers: { 'content-type': 'application/xml, text/xml', 'accept': 'application/xml, text/plain, * / *' } }) .then(function (response) { alert(response.data); }, function (error) { alert (error); }); }
thank you, soma.
Comments
Post a Comment