How to modify angular $http.get().then response invalid JSON to valid JSON

556 Views Asked by At

We need to implement type ahead functionality in inbox box ,but when we got response from $http get is invalid JSON so that i cant able do that.

below method i am using for view level

uib-typeahead="name for name in collections ($viewValue)"

Angular:

$scope.collections = function(val) {
                    return $http.get('/Documents/DocumentsList/', {
                        params : {
                            stk : val
                        }
                    }).then(
                            function(response) {
                                if (response.data.suggestions) {
                                    $("[uib-typeahead-popup].dropdown-menu").css('display','block');
                                    return response.data.suggestions
                                            .map(function(item) {
                                                return item.term;
                                            });
                                };
                            });
                };

JSON Response:

{} && {
    "name": "John",
    "age": 31,
    "city": "New York"
}

How to modify the invalid JSON to valid JSON and pass the valid response in then.

1

There are 1 best solutions below

0
On

It would be better to fix the problem at the source but, if you can't do that, implement your own response transformer

return $http.get('/Documents/DocumentsList/', {
    params: { stk: val },
    transformResponse: function(data) {
        return angular.fromJson(data.substring(6));
    }
})...