I have simple form with a textarea and a button, and I submit the form with jQuery .post() function and in ‘json’ format.
My form submits correctly until I wrote some html tags like bold etc. in the textarea, then the form dose NOT sumbit to server anymore.
I don’t know what is wrong with what I’m doing and how do I get html segments submitted using jQuery in this case?
Edit: here is the form,
<textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea>
<input name="submit" type="button" id="submit" tabindex="5" value="Submit Comment" onclick='postComment()'/>
here is the js
function postComment() {
var comment = {};
comment.Body = $("#comment").val();
$.post("Comments", comment, parseComment, 'json');
};
function parseComment(data) {
$("#commentList").html(data.Body);
};
Your posted code should work (and does work for me) provided you’re handling the comment text correctly at the server. It doesn’t do quite what it sounded like it did from your question: It sends a conventional request (not in JSON format) and returns a JSON-formatted resopnse.
If I have a very simple server-side script that receives the “Body” parameter and returns a JSON-formatted response:
…your code replaces the
commentListelement’s contents with that HTML.Here’s a JSBin that simulates what you’re doing, although of course with a static comment (since JSBin will respond to Ajax requests, but only with static content):
http://jsbin.com/usehi3
When yhou click the “Submit Comment” button, that uses your code but POSTing to http://jsbin.com/olazo4, which is just a static JSON-formatted comment:
I think the problem must lie in the server-side processing.