2009-11-22 19 views
1

我有一些基本的ajax和asp.net web服務的問題。在我的網站頁面中,我有一個文本框是到達文本編輯器,當我放入文本並嘗試提交時,ajax應該接受文本並將其傳遞給asp.net web服務。當這個句子不包含轉義字符時,它會很好,但是當它包含轉義字符時,asp.net web服務會給我提供錯誤500.在調試時,它甚至不會進入Web服務。轉義字符和錯誤500

所以問題是:我該如何解決它?

這是我的代碼。 的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() 
     } 
    }); 
} 

和Web服務看起來像這樣:

//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; 
     } 
    } 
} 

這是基本的代碼,我會添加更多的給它檢查安全。 但是現在,我需要以某種方式對文本中的轉義字符進行一些操作。 在此先感謝。

回答

2

讓jQuery的處理參數的轉義和編碼:

$.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() 
    } 
}); 

,請注意data財產。