Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular http.get doesn't like text files with square brackets at beginning and end

I have some Angular code that is grabbing a text file from S3 and then displaying it, but the http call gives an error if the text file starts and ends with something that looks like tags inside square brackets. So here is the code:

                    $http.get(url).success(function(data, status, headers, config) {
                            console.log("success " + data);
                    }).
                    error(function(data, status, headers, config) {
                            console.log("error");

                    });

and then a file which generates an error would be this:

            [image: image1.png]

            lispum iupsum oeri lispum iupsum oeri

            [image: image1.png]

It has me vexed, and does not happening if there is only a "tag" at the top of the file or at the bottom, there has to be both it seems. Remove either and things work instantly. Anyone have an idea why this might be or a workaround?

If I console.log the errors inside the error routine, the data, status and headers are all set to undefined. And I also get this Angular error:

            SyntaxError: Unexpected token i
                at Object.parse (native)
                at oc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:14:156)
                at Yb (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:77:125)
                at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:77:487
                at s (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:7:302)
                at Yc (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:77:469)
                at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:79:109)
                at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:112:276
                at l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:84)
                at l.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:123:195)
like image 442
Cocorico Avatar asked Mar 26 '26 14:03

Cocorico


1 Answers

Since the angular $http service is attempting to parse your data using JSON pattern matches, when it encounters a stream from the server which starts with a [ and ends with a ], it will interpret it as a JSON array and attempt to build an object representation of the data, which clearly isn't possible.

you can build a custom transformResponse parameter in your $http.get() to process your data in a different manner than the traditional parser. Something like this might work (note: not tested):

$http.get(url,
         {
         transformResponse: function(data){
             //normally we would take the raw data here and do transformations on it,
             //but in your situation, you don't want any transformations done.
             return data;
             }
         }).success(function(data, status, headers, config) {
             console.log("success " + data);
...

essentially, instead of allowing angular to parse the data, we are taking the transformResponse and ensuring that the data stays the way it came in.

like image 195
Claies Avatar answered Mar 28 '26 05:03

Claies