1

我正在使用 asp.net 和 jQuery 1.6

在我的函数中,我需要将 html 标签作为数据参数传递给用 C# 编写的服务器端方法。在我的数据库中,该列具有 Blob 数据类型。
我能够成功提交数据到 528b 但如果我提交大数据......我无法在服务器端发送。

当我传递小数据时,它可以工作并插入一行。但是如果我传递大约 17Kb 的数据,那么这不会转到服务器端,而是在 jquery 中提示我一个未定义的错误。

以下是 Ajax 代码:

$.ajax({
    type: "POST",
    url: "Post.aspx/savePost",
    data: "{Title:'" + Title + "',Html:'" + Html + "'}",
    contentType: "application/json",
    dataType: "json",
    success: function (data) {
        if (data == undefined) {
            alert("Error : 219");
        }
        else {
            alert(data.d);
        }
    },
    error: function (data) {
        if (data == undefined) {
            alert("Error : 465");
        }
        else {
            alert("Error : 468 " + data.d);
        }
    }
});

以下是C#代码

[System.Web.Services.WebMethod]
public static string savePost(string Title,string Html)
{
    string retMsg = "Not Saved";
    Post bp = new Post();
    int count = 0;
    count = bp.mySavePost(Title,Html);
    if (count > 0)
    {
        retMsg = "Post Save Successfully.";
    }
    return retMsg;
}

protected int mySavePost(string Title, string Html)
{
    int count=0;
    try
    {
        string rawQuery = "INSERT INTO temp_posts (Title,HTML)"
                        + " VALUES(?Title,?HTML)";
        cmd = new MySqlCommand();
        cmd.CommandText = rawQuery;
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Parameters.Add("?Title", MySqlDbType.VarChar).Value = Title;
        cmd.Parameters.Add("?Html", MySqlDbType.Blob).Value = Html;
        count = c.insertData(cmd);
     }
     catch(Exception ex){}
 }

请用你宝贵的知识指导我。
谢谢。

4

1 回答 1

1

非常感谢所有为这个线程付出最大努力的人。

最后在我的一位前辈的帮助下,我发现了我的代码中缺少的地方。

当我将 html 标签作为数据参数传递给用 C# 编写的服务器端方法时,jQuery.ajax();我需要对数据进行编码。

当我escape()在 javascript 中使用该函数对其进行编码时,它起作用了。数据被提交到数据库。

var encodedHTML = escape(Html);         //here encoding Html.
$.ajax({  
    type: "POST",   
    url: "Post.aspx/savePost",  
    data: "{Title:'" + Title + "',Html:'" + encodedHTML + "'}",  
    contentType: "application/json",  
    dataType: "json",  
    success: function (data) {  
        if (data == undefined) {  
            alert("Error : 219");  
        }  
        else {  
            alert(data.d);  
        }  
    },  
    error: function (data) {  
        if (data == undefined) {  
            alert("Error : 465");  
        }  
        else {  
            alert("Error : 468 " + data.d);  
        }  
    }  
});  

谢谢大家 :)

于 2012-07-24T10:41:04.153 回答