0

The back end guy wants to send me a string, which will be returned by $http.post. If he sends me something like "success", I will get error like "parsing Json failed". I want him to wrap the string into object like "{"message": "success"}", which works fine. But other back end guys say that front end should comply with back end, so they will just send me the string. Is there any way that I can read the string?

This is the code I use if he sends me "{"message": "success"}", which works perfectly:

AdminService.saveCache(cache)
    .then(function(data) {
        var data = data.data;

        if (data.message == "success") {
            scope.successMessage = "Cache hours saved successfully";
        } else {
            scope.errorMessage = data.message;
        }
    }, function() {
        scope.errorMessage = "Submission failed";
    });
9
  • 1
    AFAIK, Angular only parses the returned string as JSON if the response content type is application/json. And "success" is not valid JSON. So, if that's indeed the case, you can tell the guy that the response it sends is incorrect. He should set the content type to text/plain. Commented Feb 23, 2016 at 17:43
  • Are they returning 200 OK responses no matter what? It seems to me that returning 4xx responses for invalid requests is better. Then $http can properly resolve/reject the promise appropriately. Commented Feb 23, 2016 at 17:44
  • 1
    Moreover, if there is an error, the guy shouldn't send back a response with a status 200 (which means success). The status should be 4xx or 5xx. Commented Feb 23, 2016 at 17:44
  • @JBNizet They don't know about front end. I tried to explain that. They just don't understand. That's why they send me error message when there is an error. Commented Feb 23, 2016 at 17:47
  • @ryanyuyu Yes, if they get something error in back end, they still send me 200 OK with error message. Commented Feb 23, 2016 at 17:48

2 Answers 2

1

By default angular tries to detect if a http response contains JSON. Sometimes this detection fails and you get such an error as you described in your question.

You can avoid this behavior for a single request if you override the response transformation by providing an transformResponse property for on the configuration object passed to the request:

$http({
  url: '...',
  method: 'POST',
  //Just return original response from server without parsing
  transformResponse: [function (data, headers) {
        return data;
    }];
});

Alternatively you can change the default behavior for all your app's http requests by overriding the default response transformation:

myApp.config('$httpProvider', function($httpProvider) {
    $httpProvider.defaults.transformResponse = [function (data, headers) {
        return data;
    }];
}]);

For more information see API Reference $http section "Transforming Requests and Responses"

Sign up to request clarification or add additional context in comments.

Comments

0

the API Response Content-Type should be set to text/plain; and use POSTMAN to verify, it would save you a lot of headache

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.