I’m saving a text with jquery and json.
I’m getting the text of a textarea with .text() and saving it with json.
All special chars except \ and ‘ are saved.
If I try to save %&$§/////blaääüü**´~+ it works, but if i try to save
%&$§/////blaääüü**´~+\\\\\\\''''' it does not work!
How can i solve this?
Edit
The code for the saving is: (extract)
$.ajax({
url: "myURI"
, type: "POST"
, dataType: "json"
, data:
"{ " +
"', 'text': '" + $('#myText').text() +
"' }"
, contentType: "application/json; charset=utf-8";
});
Ok, your main problem is that the JSON you create is not valid and that you create it “manually”. The result of your string concatenation is :
which does not really look like JSON.
Use
JSON.stringify[MDN] (external library available as well) instead:Further issues:
Keys in JSON must be in double quotes, not single quotes.
The fact that the text contains the escape character (
\) and single quotes (') messes up with the final string you create. That’s why you should leave proper escaping of the content and the creation to the browser.Depending on the server side language you use, you should assign the JSON to a key, so that a proper key-value pair is sent to the server: