pinksy
pinksy

Reputation: 311

Angular $http.post not returning XML

I am trying to call a web service with angular, but not having much luck. The service takes a POST request with no POST body, and returns XML. I can confirm that the service works with a raw XMLHttpRequest call:

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:

$.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.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:

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. If I include the parameter as null, Chrome says:

Request header field Content-Type is not allowed by Access-Control-Allow-Headers

Since the $http.post shortcut method does not complain about missing out the data parameter, I have deliberately missed it out.

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. 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)

Upvotes: 0

Views: 1764

Answers (1)

sylwester
sylwester

Reputation: 16498

Angular by default expecting to get JSON from your server you can change that by adding

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

Upvotes: 5

Related Questions