如何在Angular $ http响应中捕获格式错误的JSON?

时间:2014-02-15 22:29:24

标签: javascript json angularjs http amazon-s3

更新:使用基于Eamonn Gahan的答案的解决方案更新代码

我正在尝试获取一些用户生成的文件,这些文件无法被信任以正确格式化。我认为这很简单,但是当我的回答有点像JSON时,我很难做什么,但事实并非如此。

例如,如果服务器响应:a simple string {with brackets}一切都按预期工作。但是,如果服务器响应:{ not_json: { malformed on purpose },那么我无法弄清楚如何访问响应,无论是成功还是失败响应。

似乎发生的是Angular JSON解析器抛出语法错误而不拒绝$ http promise。

以下是一些突出问题的代码。

var myApp = angular.module('myApp', []);

function MyCtrl($scope, $http){

    // --- Added this block based on Eamonn Gahan's answer
    function txfmParseText(data, hdrGetter){
        var contentType = hdrGetter()['content-type'];
        if ( contentType.match(/^text\/plain/) ){
            data = JSON.stringify(data);
        } 
        return data ;
    }

    $http.defaults.transformResponse.unshift(txfmParseText);
    // --- end of updated block

    $scope.sanityCheck="Alive";

    // URL returns (as text/plain):
    //   { not_json: { malformed on purpose }
    var urlOpts1 = {
        method: 'GET',
        url: 'https://s3.amazonaws.com/forforf-cdn/not_json_test'
    };

    // URL returns (as text/plain):
    //   a simple string {with brackets}
    var urlOpts2 = {
        method: 'GET',
        url: 'https://s3.amazonaws.com/forforf-cdn/simple_string_with_brackets'
    };

    // First request fails  [update: passes with transformResponse function]
    $http(urlOpts1)
    .then(function(resp){
        //never gets here
        console.log(resp);
        $scope.notJson = resp.data;
    })
    .catch(function(err){
        //nor is the promise rejected
        console.log(err)
    });

    // Second request works as expected
    $http(urlOpts2).then(function(resp){
        //works as expected
        console.log(resp);
        $scope.stringWithBrackets = resp.data;
    });

}

jsFiddle

已更新 jsFiddle with solution

这是失败的请求和响应的HTTP。数据由S3提供,数据的内容类型设置为text/plain

Request URL:https://s3.amazonaws.com/forforf-cdn/not_json_test
Request Method:GET
Status Code:304 Not Modified

Request Headers
GET /forforf-cdn/not_json_test HTTP/1.1
Host: s3.amazonaws.com
Connection: keep-alive
Cache-Control: max-age=0
Accept: application/json, text/plain, */*
Origin: http://fiddle.jshell.net
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
Referer: http://fiddle.jshell.net/_display/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "84cf04d01a85ed58af77293ea7b1884a"
If-Modified-Since: Sat, 15 Feb 2014 20:52:30 GMT

Response Headers
HTTP/1.1 304 Not Modified
x-amz-id-2: FvdtpH6WmRvEAFIt3clAsXC133iyGQ/Qezlzt/5P6UDFbZvDfUC7WeuPv+re0ywE
x-amz-request-id: FA51FED4F6A70DA2
Date: Sat, 15 Feb 2014 22:02:22 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Last-Modified: Sat, 15 Feb 2014 20:52:30 GMT
ETag: "84cf04d01a85ed58af77293ea7b1884a"
Server: AmazonS3

认为可能是S3没有设置正确的Content-Type,我也有S3响应: Content-Type: text/plain; charset="utf-8",但没有帮助。

有人有什么想法吗?

1 个答案:

答案 0 :(得分:3)

这基本上发生了,因为$ http响应上的角度是通过$httpProvider.defaults.transformRequest属性上的函数数组传递的。正如您在此处所见:https://github.com/angular/angular.js/blob/7aef2d54e0a48fae18a289813f699962d8310565/src/ng/http.js#L94数组中有一个默认函数。此函数检查一些基本的正则表达式,以查看它是否应该响应JSON.parse()。遗憾的是,这个格式错误会通过但您可以在配置块中覆盖transformResponse属性,或者您可以在实际的$http调用中指定transformResponse属性,如下所示(来自您的小提琴):

var urlOpts1 = { method: 'GET', url: 'https://s3.amazonaws.com/forforf-cdn/not_json_test', transformResponse: specialTransform };

http://jsfiddle.net/GBD2v/

在上面的小提琴中,您可以看到“{not_json:{malformed on purpose}”被打印为常规字符串,因为我们只是在自定义transformResponse函数中直接返回字符串。