I am working with a JavaScript string that looks like this:
user_fav = "21,16";
I need to process this string through a function so that it becomes a JSON array with an id
key, like this:
{"id":21},{"id":16}
This JSON array is then used in an $http request:
return $http({
method: 'GET',
url: getUrl('products'),
params: {
pageSize: 2000,
pageNumber: 1,
"filter": { "q": { "$or": [{"id":21},{"id":16}] } }, // <-- HERE
sort: []
}
});
When I send the original string user_fav
in the $http request, everything works fine. However, when I convert the string into JSON using the converter below and send it in the $http request, it causes an error:
user_fav = "21,16";
var user_fav_to_array = "";
var splitFav = user_fav.split(",");
for (var i = 0; i < splitFav.length; i++) {
user_fav_to_array += JSON.stringify({ id: parseInt(splitFav[i]) }) + ',';
}
var JSONFavs = user_fav_to_array.substring(0, user_fav_to_array.length - 1);
//Result: JSONFavs => {"id":21},{"id":16}
The error occurs in this part of the code:
return $http({
method: 'GET',
url: getUrl('products'),
params: {
pageSize: 2000,
pageNumber: 1,
"filter": { "q": { "$or": [JSONFavs] } }, // <-- HERE
sort: []
}
});
The error message is 417 (Critical Exception)
, and it is generated by the Backand.com system.