I have a JSON array:
info = [{"name":"abc", "rank" : 1},{"name":"xyz", "rank":2},{"name":"pqr", "rank":3}];
and I am trying to pass it as an input value to a hidden field using jQuery
and send it to the server with a POST
request.
$('<form action="/info/saveAll" method="POST"/>')
.append($('<input type="hidden" name="info" value="' + JSON.stringify(info) + '">'))
.appendTo($(document.body))
.submit();
At the server end, I am accessing the value of info
as:
router.route('/saveAll')
.post((req, res) => {
let info = JSON.parse(req.body.info);
console.log(info); //SyntaxError: Unexpected end of JSON input
})
If I don't stringify
the array before submit, then the typeof info
still returns string inside the post and when I try to parse
the string type I get syntax error like SyntaxError: Unexpected token o in JSON at position 1
.
I know this can be done by just doing it through an ajax post request, but I wanted a work around that does not involve an ajax request.
Any help would be great.