I am sending some data from an server back to a page. It looks like valid JSON to me, yet, it is being caught/handled by the error handler in my jQuery.ajax() call.
This is the error message displayed:
Error: Invalid JSON: ({"id":"#settingsResult","payload":"<form id=\"pwd_change_frm\" action=\"post\">\n <div>\n <div>\n <div><\/div>\n <div><label for=\"changepwd_password\">New Password<\/label><\/div>\n <div><input type=\"password\" name=\"changepwd[password]\" title=\"Enter new password\" style=\"width:258px;\" id=\"changepwd_password\" \/><\/div>\n <\/div>\n <div class=\"spacer\"> <\/div>\n <div>\n <div><\/div>\n <div><label for=\"changepwd_password_confirm\">Confirm New Password<\/label><\/div>\n <div><input type=\"password\" name=\"changepwd[password_confirm]\" title=\"Retype new password)\" style=\"width:258px;\" id=\"changepwd_password_confirm\" \/><\/div>\n <\/div>\n <div class=\"spacer\"><\/div>\n <div><img id=\"pwd_chg_btn\" class=\"submit_btn\" src=\"\/images\/button_submit.gif\" alt=\"Button_submit\" \/><\/div>\n <\/div>\n <input type=\"hidden\" name=\"changepwd[_csrf_token]\" value=\"1b7f3529797245c0fc43c3ddf5ade30d\" id=\"changepwd__csrf_token\" \/><\/form>\n<div class=\"spacer\"><\/div>"})
As an aside, FF Firebug correctly parses the returned data and displays ot correctly - which is why I dont understand why jQuery can't seem to handle it.
here is the bit of code that makes the call:
jQuery.ajax({
type: 'POST',
url: '/some_url?id='+this.id,
timeout: 2000,
success: function(result){ jQuery(result.id).html(result.payload); },
error: function (xhr, ajaxOptions, thrownError){ alert('Error: '+ thrownError); }
});
Probably useless to answer at this point, but I just thought I'd note that the issue does somehow seem to be with $.parseJSON().
If I remove all occurrences of \n, and replace all occurrences of \" with \', it works.
Alternatively, if I double escape them, it works.
\\n
\\"
...which would make sense since I believe jQuery simply does an eval() (or effectively the same thing), so I guess escaping the " is terminating the string and escaping the \n is introducing a newline character where it would be invalid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With