如何修改angular $ http.get()。然后响应无效的JSON为有效的JSON

如何修改angular $ http.get()。然后响应无效的JSON为有效的JSON

问题描述:

我们需要在收件箱中实现类型提前功能,但是当我们从$ http得到响应get无效JSON,所以我无法做到那。

以下方法,我使用视图级

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

角:

$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响应:

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

如何修改无效JSON到瓦利d JSON,然后传递有效的响应。

+0

你为什么不解决实际的端点响应所以它是有效的JSON? – Phil

+0

在您的JSON响应中没有看到“建议”。你如何期望它能够工作? – Phil

+0

@菲尔:是的,你是对的。提供的json不是actula响应JSON唯一的示例格式 – CodeMan

这将是更好的解决在源头的问题,但是,如果你不能做到这一点,实现自己的响应变压器

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