So I have some code that posts this JSON:
post = {
period: "1",
data: {1: "Here"}
}
And it is posted with this code:
$.ajax({
url: 'http://127.0.0.1:5000/',
data: post,
type: 'POST'
})
When I execute this code, my server receives this string: "period=1&data%5B3%5D=Here"
Is there a way to convert this string back into a JSON?
I have tried JSON.parse() but that obviously did not work.
It was never JSON, you can send it as JSON by using JSON.stringify and then use JSON.parse to get it back as an object on the server.
$.ajax({
url: 'http://127.0.0.1:5000/',
data: JSON.stringify(post),
contentType: 'application/json',
type: 'POST'
})
You can parse the string with the URLSearchParams constructor, then use Object.fromEntries to convert it to an object:
const obj = Object.fromEntries(new URLSearchParams("period=1&data%5B3%5D=Here"));
console.log(obj)