Angular $ http.post不返回XML

时间:2015-03-16 12:42:54

标签: javascript angularjs

我试图用角度调用Web服务,但没有太多运气。该服务接受没有POST主体的POST请求,并返回XML。我可以确认该服务使用原始XMLHttpRequest调用:

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
  if(xhr.readyState == 4)
    console.log(xhr.responseText); // Returns data
}

xhr.open("POST", "https://api.bmreports.com/BMRS/MessageListRetrieval/v1/?APIKey=9eu73tsryf1sons&ParticipantId=INNOGY01&PublicationFrom=1970-01-01&PublicationTo=3000-01-01&ServiceType=XML", true);

xhr.send(null);

使用jQuery:

$.ajax({
  url: 'https://api.bmreports.com/BMRS/MessageListRetrieval/v1/?APIKey=9eu73tsryf1sons&ParticipantId=INNOGY01&PublicationFrom=1970-01-01&PublicationTo=3000-01-01&ServiceType=XML',
  type: "POST",
  success: function(data){
    console.log(data); // Returns data
  },
  error: function (hxr, status, errorThrown){
    console.log(status);
  }
});

但是,当我使用有角度的$ http服务尝试时,我没有收到任何回复:

angular.module('TestApp',[])
.controller('TestController', function($scope, $http){
    $http({
        method: "POST",
        url: 'https://api.bmreports.com/BMRS/MessageListRetrieval/v1/?APIKey=9eu73tsryf1sons&ParticipantId=INNOGY01&PublicationFrom=1970-01-01&PublicationTo=3000-01-01&ServiceType=XML'
    }).success(function(data, status, headers, config){
        console.log("data:");
        console.log(data); // Returns null
    }).error(function(data, status, headers, config){
        console.log("error status:");
        console.log(status); // No errors returned      
    })
})

编辑:使用$ http.post快捷方式:

angular.module('TestApp',[])
.controller('TestController', function($scope, $http){
    $http.post(
        'https://api.bmreports.com/BMRS/MessageListRetrieval/v1/?APIKey=9eu73tsryf1sons&ParticipantId=INNOGY01&PublicationFrom=1970-01-01&PublicationTo=3000-01-01&ServiceType=XML'
    ).success(function(data, status, headers, config){
        console.log("data:");
        console.log(data);
    }).error(function(data, status, headers, config){
        console.log("error status:");
        console.log(status);        
    })
})

请注意,$ http.post快捷方式有第二个数据参数,但我没有要传递的数据。如果我将参数包含为null,Chrome会说:

  

请求标头字段Access-Control-Allow-Headers

不允许使用Content-Type

由于$ http.post快捷方式没有抱怨错过了数据参数,我故意错过了它。

我需要能够在没有数据的情况下进行POST调用,就像使用原始XMLHttpRequest调用或jQuery的ajax方法一样。可能出了什么问题?谢谢!

(注意,API是公开的,所以不要担心我发布的API密钥。它是一个有效的API密钥,我只会在这个问题上保持活动状态是开放的)

1 个答案:

答案 0 :(得分:5)

Angular默认希望从您的服务器获取JSON,您可以通过添加

来更改它

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

app.controller('homeCtrl', function($scope, $http) {

  $scope.xml = "";


  $http({
    method: "POST",
    url: 'https://api.bmreports.com/BMRS/MessageListRetrieval/v1/?APIKey=9eu73tsryf1sons&ParticipantId=INNOGY01&PublicationFrom=1970-01-01&PublicationTo=3000-01-01&ServiceType=XML',
    headers: {
      "Accept": "application/xml"
    }
  }).success(function(data, status, headers, config) {
    console.log("data:");
    console.log(data); // Returns null
    $scope.xml = data;
  }).error(function(data, status, headers, config) {
    console.log("error status:");
    console.log(status); // No errors returned      
  })


});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <div ng-controller="homeCtrl">
   {{xml}}
  </div>
</div>

根据您的要求