While working with data from an external web-service, which I have no control over, I encountered a problem where names containing apostrophes were causing issues due to backslashes being added in the JSON response. JSLint.com confirms that the JSON structure is valid but it breaks when handling these backslashed apostrophes.
I have reached out to the provider of the web service for assistance. In the meantime, as I await a response from ReallyBigCo, I am exploring potential solutions on my end to manipulate the request.
Is there a method to remove characters from a JSON object during an $http.get() request?
I have been researching ways to extract the response as a string in order to clean up the unwanted characters. However, I am encountering errors upon completion of the $http.get() function.
The following AngularJS code functions correctly (for valid JSON responses):
var app = angular.module('app',[]);
app.factory('appresults', function($http) {
return {
getAsync: function(callback) {
var myURL = https://address-to-really-big-co-webservice.com;
$http.get(myURL).success(callback);
}
};
});
app.controller('appcontroller', function($scope, appresults) {
appresults.getAsync(function(results) {
$scope.appdata = results.findUsers;
});
});
I can populate a list of emails on my page using this code when the JSON is valid:
<p ng-repeat="item in appdata">{{item.email}}</p>
Here is an example showcasing successful JSON results:
{"findUsers":[
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8999999d68b97959d9a979c81b89c9795999196d69b9795">[email protected]</a>"},
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccaeaeaee2bfa3a1a9aea3a8b58ca8a3a1ada5a2e2afa3a1">[email protected]</a>"},
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7c4c4c489d4c8cac2c5c8c3dee7c3c8cac6cec989c4c8ca">[email protected]</a>"}
]}
However, the JSON results below trigger an error:
{"findUsers":[
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="315050501f425e5c54535e554871555e5c50585f1f525e5c">[email protected]</a>"},
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254747470b564a4840474a415c65414a48444c4b0b464a48">[email protected]</a>"},
{"email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2e2e2e633e2220282f2229340d2922202c2423632e2220">[email protected]</a>"},
{"email":"ddd.o\'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="45362a2820272a213c05212a28242c2b6b262a28">[email protected]</a>"}
]}
This issue with apostrophes leads to an error message in the console specifically in IE9:
SyntaxError: Invalid characterundefined
I have been looking into methods for restructuring the response, but any JSON results containing an apostrophe consistently triggers this error. Therefore, employing simple solutions like results.replace(/'/g,"");
seems unfeasible unless I find a way to address the object prior to encountering the error.