开发者

Escape character and error 500

开发者 https://www.devze.com 2022-12-12 18:41 出处:网络
I have a problem with some basic ajax and asp.net web service. In my website page i have a text box that is reach text editor, when i put text and try to submit it, ajax supposed to take the text and

I have a problem with some basic ajax and asp.net web service. In my website page i have a text box that is reach text editor, when i put text and try to submit it, ajax supposed to take the text and pass it to asp.net web service. When the sentence contains no escape characters it goes well, however when it does contains escape character the asp.net web service gives me error 500. On debug it doesn't even enters into web service.

So the question is: How can i fix it ?

Here is the code that i have. Javascript:

//posting the user comment
function postComment() {
    var comment_body = $("textarea[id*='txt_editor']").val();
    $.ajax({
        type: "POST",
        url: "Article.asmx/postComment",
        data: "{'article_id': '" + article_id + "', 'comment_body' : '" + comment_body + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            page_num = 1;
            getComments();
            clearComment()
        }
    });
}

And the web service looks like that:

 //posting the comment to database
    [WebMethod]
    public int postComment(int article_id, string comment_body)
    {
        try
        {
            using (ForMarieDataContext forMarie = new ForMarieDataContext())
            {
                tbl_article_comment newComment = new tbl_article_comment();
                newComment.article_id = article_id;
                newComment.comment_author = 开发者_运维知识库"Dmitri";
                newComment.comment_date = DateTime.Now.ToString();
                newComment.comment_body = comment_body;

                forMarie.tbl_article_comments.InsertOnSubmit(newComment);
                forMarie.SubmitChanges();

            }

            return 1;
        }
        catch(Exception ex)
        {
            return 0;   
        }
    }
}

This is the basic code and i will add more to it to check for security. However for now, i need to somehow do something with the escape characters in text. Thanks in advance.


Let jQuery handle the escaping and encoding of the parameters:

$.ajax({
    type: "POST",
    url: "Article.asmx/postComment",
    data: { article_id: article_id, comment_body: comment_body },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        page_num = 1;
        getComments();
        clearComment()
    }
});

Pay attention to the data property.

0

精彩评论

暂无评论...
验证码 换一张
取 消