views:

42

answers:

2

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);
};
+1  A: 

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:

{"Body": "This is the <strong>comment</strong>"}

...your code replaces the commentList element'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:

{"Body": "This is <strong>boldfaced</strong>, this is <em>italics</em>"}

I think the problem must lie in the server-side processing.

T.J. Crowder
Thanks a lot Crowder. But I tried to debug it, when I inputted <b>test</b> the server side doesn't break, so I'm ruling out it's the server side processing.
ray247
@ray247: The only real answer is to walk through with a debugger, and double-check exactly what's going between the server and the client. The code you've shown works, so the problem lies elsewhere.
T.J. Crowder
@Crowder: Hi Crowder, you are right the client side is correct and it did reach the server side but just not the action method I set the breakpoint in. Since I'm using ASP.NET MVC, it needs [ValidateInput(false)] on the action method which I literally thought I tried while the breakpoint still didn't hit, but this morning it did break into the method after I added the attribute again. Thanks for your help and I'm marking yours as the answer.
ray247
@ray247: I'm glad you got it sorted out! Much as it's nice to have credit for the answer, probably best to post your own answer saying what you said above and then accept that instead (you'll probably have to wait a day before you can do that), since although I helped ensure the client-side wasn't the problem, the actual *answer* to your problem was the ASP.Net validation. (I wouldn't turn down an upvote instead, though :-) )
T.J. Crowder
A: 

If you are entering new lines in TextArea, it will automatically create New Line character "\n". These character used to make problem in Javascript. Try removing those characters.

var strMultiLineText = 
    "Karen didn't know how to feel any more. She had never liked girls\n" +
    "\"that way,\" but she couldn't ignore the emotion, that surge of \n" +
    "adrenaline that coursed through her body at the sight of Kimmie's\n" +
    "wet, naked, voluptuous figure in the locker room showers.";
// Strip out all line breaks.

var strSingleLineText = strMultiLineText.replace(
    // Replace out the new line character.
    new RegExp( "\\n", "g" ), 

    // Put in ... so we can see a visual representation of where
    // the new line characters were replaced out.
    " ... " 
    );

// Alert the new single-line text value.

You can check this blog for more details. http://www.bennadel.com/blog/161-Ask-Ben-Javascript-Replace-And-Multiple-Lines-Line-Breaks.htm.

Muneer