简体   繁体   English

在浏览器中工作时,Angular HTTP GET请求返回未定义

[英]Angular HTTP GET request returns undefined while working in browser

I'am learning AngularJs and I've tried to write a very basic script sending an http request to Ebay public API, I've signed up and got my API keys, I've read the docs several times and wrote this basic code : 我正在学习AngularJs,并且尝试编写一个非常基本的脚本来向Ebay公共API发送一个http请求,我已经注册并获得了我的API密钥,我已经阅读了几次文档并编写了这个基本代码:

 $scope.getQueryUrl = function () {
    // Some 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 the query url after adding all the parameters
    alert($scope.queryUrl.Ebay);
    $http.get($scope.queryUrl["Ebay"]).then(
        function(response){
            alert("success" + response.data );
        },
        function(response){
            alert("error"   + response.statusCode );
        });


};

How this code should work : 此代码应如何工作:

It should create a formated Ebay query url, send it through HTTP GET request and sending back the response . 它应该创建一个格式化的Ebay查询URL,通过HTTP GET请求将其发送并发送回响应。

Note : $scope.qtext & dataAuth.EbayKeyApi are already assigned with their respective values . 注意: $scope.qtextdataAuth.EbayKeyApi已经分配了各自的值。

What's the problem : 有什么问题

The problem is that using this Angularjs script, the code doesn't work, the alert "Error" is shown, and the response.statusCode is undefined . 问题在于,使用此Angularjs脚本,代码不起作用,显示警告“错误”,并且response.statusCode未定义。

But when I copy the formatted Ebay query link in Firefox it works perfectly and the XML response is shown . 但是,当我在Firefox中复制格式化的Ebay查询链接时,它可以正常工作并显示XML响应。

The formatted Ebay query was generated using the script provided . 格式化的Ebay查询是使用提供的脚本生成的。

I think it's a header related problem . 我认为这是与标头相关的问题。

$http has some default headers defined. $ http定义了一些默认标题。 $http sends Json payload and accepts Json as the response by default. $ http默认发送Json有效负载并接受Json作为响应。 Since you are dealing with XML you have to explicitly specify the accepted response type as XML using the header: Accept: application/xml 由于您正在处理XML,因此必须使用标头将接受的响应类型明确指定为XML:Accept:application / xml

Please use the following function with appropriate headers and you should get the response. 请使用以下带有适当标题的函数,您应该会得到响应。 Also, please look into any Cross Origin Resource Sharing (CORS) restrictions on the ebay API. 另外,请查看eBay API上的任何跨源资源共享(CORS)限制。

    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. 谢谢你索玛

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

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