简体   繁体   English

角$ http.post不返回XML

[英]Angular $http.post not returning XML

I am trying to call a web service with angular, but not having much luck. 我试图以有角度的方式调用Web服务,但运气不佳。 The service takes a POST request with no POST body, and returns XML. 该服务接受没有POST正文的POST请求,并返回XML。 I can confirm that the service works with a raw XMLHttpRequest call: 我可以确认该服务可与原始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);

And with jQuery: 并使用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);
  }
});

However, I'm not getting anything back when I try it with angular's $http service: 但是,当我使用angular的$ 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      
    })
})

EDIT: Using the $http.post shortcut method: 编辑:使用$ 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);        
    })
})

Note that the $http.post shortcut method has a second data parameter, but I have no data to pass. 请注意,$ http.post快捷方式方法具有第二个数据参数,但是我没有要传递的数据。 If I include the parameter as null, Chrome says: 如果我将参数包含为null,Chrome会说:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers Access-Control-Allow-Headers不允许请求标头字段Content-Type

Since the $http.post shortcut method does not complain about missing out the data parameter, I have deliberately missed it out. 由于$ http.post快捷方式方法不会抱怨缺少data参数,因此我故意将其遗漏了。

I need to be able to make the POST call with no data, as is possible with a raw XMLHttpRequest call, or jQuery's ajax method. 我需要能够不使用数据进行POST调用,这可以通过原始XMLHttpRequest调用或jQuery的ajax方法实现。 What might be going wrong? 可能出什么问题了? Thanks! 谢谢!

(NB, the API is public, so don't worry about the API key I've posted. It's a valid API key, which I'll keep active only while this question is open) (注意,该API是公共的,因此不必担心我发布的API密钥。这是有效的API密钥,只有在打开此问题时,我才会保持活动状态)

Angular by default expecting to get JSON from your server you can change that by adding 默认情况下,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> 

to your request 根据您的要求

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

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