【问题标题】:How to modify angular $http.get().then response invalid JSON to valid JSON如何修改角度 $http.get().then 将无效 JSON 响应为有效 JSON
【发布时间】:2016-12-30 04:54:31
【问题描述】:

我们需要在收件箱中实现提前输入功能,但是当我们从 $http 得到响应时,得到的 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 修改为有效的 JSON,然后传递有效的响应。

【问题讨论】:

  • 为什么不修复实际的端点响应,使其成为有效的 JSON?
  • 在您的 JSON 响应中没有看到 suggestions。您希望它如何工作?
  • @Phil:是的,你是对的。提供的 json 不是实际响应 JSON,它是唯一的示例格式

标签: angularjs json angularjs-scope typehead


【解决方案1】:

最好从源头解决问题,但如果你不能这样做,请实现自己的响应转换器

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多