I'm trying to send a string array as a parameter on a get request.
console.log(arrayOfStrings); //Prints ["28"]
var ids = JSON.stringify(arrayOfStrings);
console.log(ids); //Prints ["\u00002\u00008"]
$http.get('do_staff_search', { params:{'ids': ids } }).then(successHandler);
However, when I stringify the number array, I get ["\u00002\u00008"]
which then causes an error on the server java.lang.NumberFormatException: For input string: "▯2▯8"
with the two rectangular blocks in front of each number.
If I use Google Chrome's console, create the same array and stringify it, the output is "["28"]"
This seems like a trivial issue, but I couldn't find a good similar question on Stack Overflow.
UPDATE
I did some tests and it turns out @MinusFour is correct. It is an array of strings, not an array of integers as I assumed (the array is the payload from another request).
UPDATE 2
I tried converting the string array to an integer array using this function:
function arrayOfNums(arr){
var newArr = [];
for (var i = 0; i < arr.length; i++) {
newArr[i] = parseInt(arr[i]);
};
return newArr;
}
But parse Int is returning NaN for each element. Now i'm wondering if there is some encoding issue with my strings that cold be causing it, since I got them from a server request I made earlier. I found this related question but I'm not sure how I would escape any invalid characters.
Just as some background, the array is stored as a CLOB on an SQL DB. I'm using Spring and Jackson on the server side to send a JSON object back, and within this object I have the array in question. Although I have access to the code on the server, I can't really change it because there are other applications that make requests to it.
It seems the strings are coming with some invalid characters back from the AJAX request as described here
So before running the array through JSON.stringify
, I'm cleaning each string up like this:
function arrayOfNums(arr){
var numberPattern = /\d+/g;
var newArr = [];
for (var i = 0; i < arr.length; i++) {
newArr[i] = parseInt(arr[i].match( numberPattern ).join(""));
};
return newArr;
}
Because there are invalid characters in front of each digit, I use join to concatenate all digits together after matching the pattern.
More of a work around than a permanent solution, I just hope this helps someone in a similar situation.