I have a PHP script that returns a json_encoded response that is like so:
//PHP
$response = array('type' => 'success', 'content' => '&&!%$#!some words');
echo json_encode($response);
return;
Now the JS takes the response and tries to put the content in a textarea:
$('#some_form').ajaxForm({
success: function(resp){
if(resp.type === 'success')
{
$('#text_area').val(resp.content);
}
},
dataType: 'json'
});
The content of the script will be displayed as this in the text area:
&&!%$#!some words
Why are the ampersands being messed up but not the other types of punctuation? Is there a way around this? I would like the ampersands to show up as a regular ampersand in the text area.
Just looked through the code for the jquery form plugin. There is a special case in there where file uploads are treated differently (and the part that I’m using as well). Details of how that works can be found here: http://jquery.malsup.com/form/#file-upload
But what happens is that it uses a call to innerHTML to retreive the content. That actually converts things into html character strings.
I augmented it in a way so that it does a URL encode first then URL decode to maintain the integrity of the text.