Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you might be hitting a subtle issue with <code>$.post()</code> 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 <code>application/x-www-form-urlencoded</code> string.</p> <p>Another issue with <code>$.post()</code> is that it always sends a content-type header of <code>application/x-www-form-urlencoded; charset=UTF-8</code>. This might be the reason why the server is crashing: it's expecting JSON, but the content-type is <code>application/x-www-form-urlencoded</code>. To fix this we can use the more general <code>$.ajax()</code> which has more options.</p> <p>Try this:</p> <pre><code>$.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);} }); }); </code></pre> <p>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:</p> <pre><code>$.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);} }); } }); </code></pre> <p>Please try both!</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload