I think you might be hitting a subtle issue with $.post()
when the data is an object. It gets converted to a string, but not to a JSON string - rather, it is used as a set of key-value pairs for building a application/x-www-form-urlencoded
string.
Another issue with $.post()
is that it always sends a content-type header of application/x-www-form-urlencoded; charset=UTF-8
. This might be the reason why the server is crashing: it's expecting JSON, but the content-type is application/x-www-form-urlencoded
. To fix this we can use the more general $.ajax()
which has more options.
Try this:
$.getJSON('/assets/appconf/db_trial.json', function(firstData)
{
$.ajax({
type: "POST",
url: '/createDBJSON',
data: JSON.stringify(firstData), // send JSON representation
contentType: 'application/json; charset=utf-8', // set correct content-type header
success: function(secondData) {window.alert(secondData);}
});
});
We could also try another method which never bothers to convert the first json response into an object and then back into a json string, instead just keep it as text the whole time:
$.ajax({
dataType: "text", // parse response only as text
url: '/assets/appconf/db_trial.json',
success: function(jsonString) {
$.ajax({
type: "POST",
url: '/createDBJSON',
data: jsonString, // jsonString is already a json string
contentType: 'application/json; charset=utf-8', // set correct content-type header
success: function(secondData) {window.alert(secondData);}
});
}
});
Please try both!
But I still recommend fixing your back-end so the data doesn't pass through the client for no reason! I think Play Framework 2 includes Jackson for JSON manipulation, so you just need to import the right classes, and search the internet to learn how to read JSON files with the Jackson library.