Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$http.get and null responses from endpoints #2191

Closed
JamieMason opened this issue Mar 20, 2013 · 5 comments
Closed

$http.get and null responses from endpoints #2191

JamieMason opened this issue Mar 20, 2013 · 5 comments

Comments

@JamieMason
Copy link
Contributor

$http.get(endpoint).then(function(response) {
// here...
});

If the response body is {"yum":"dinner"} then response.data is an Object, fine.

If the server response body is null then response.data is a String "null".

Should it not be an actual null, since JSON.parse("null") === null?

Thanks for your time, please address/close as desired.

@btford btford closed this as completed Aug 24, 2013
@btford
Copy link
Contributor

btford commented Aug 24, 2013

As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months.

Please try the newest versions of Angular (1.0.8 and 1.2.0-rc.1), and if the issue persists, comment below so we can discuss it.

Thanks!

@twhitbeck
Copy link
Contributor

The issue persists for 1.0.8. The result is just the string "null" and the contentType header is "applcation/json". What I get is the string "null" instead of a null object. If I call angular.fromJson myself, I get the expected null object.

@kcrwfrd
Copy link

kcrwfrd commented Nov 22, 2013

Still experiencing this issue in 1.2.1

@btford btford reopened this Nov 22, 2013
@kcrwfrd
Copy link

kcrwfrd commented Nov 22, 2013

Looks like it's not being interpreted as JSON because it doesn't pass the test on https://github.com/angular/angular.js/blob/master/src/ng/http.js#L98 because it doesn't start or end with brackets: {} or []

In the meantime I've added another default transformRequest function that checks for a null response:

angular.module('myApp', []).config(['$httpProvider',
  function($httpProvider) {
    $httpProvider.defaults.transformResponse.push(function(data){
      if (data === "null") {
        data = null;
      }
      return data;
    });
  }
]);

This doesn't take into account any whitespace but it's sufficient for my purposes.

@pkozlowski-opensource
Copy link
Member

Yes, this is the same as #2973. Closing this one as duplicate of #2973 since there is a PR opened for #2973 already.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants