使用jquery.ajax传递大数据 [英] Pass Large data using jquery.ajax

查看:573
本文介绍了使用jquery.ajax传递大数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在使用asp.net和jQuery 1.6

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

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

以下是Ajax代码:

Hello Friends,

I am using asp.net and jQuery 1.6

In my function I am required to pass html tags as a data parameter to my server side method written in C#. In my Database the column has Blob datatype.
I am able to submit data successfully upto 528b but If I submit large data... I''m not able to send at server side.

When I pass small data it works and a row inserted. But If I pass data around 17Kb then this doesn''t go to the sever side, but prompt me an undefined error in jquery.

Following is Ajax Code:

$.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#代码



C# Code

[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){}
 }




请为我提供宝贵的知识.
谢谢.




Please guide me with you valuable knowledge.
Thanks.

推荐答案

.ajax({ 类型:" , url:" , 数据:" +标题+ " ',HTML:'" + HTML + , contentType:" , dataType:" , 成功:功能(数据){ 如果(数据== 未定义){ alert(" ); } 其他 { 警报(data.d); } }, 错误:功能(数据){ 如果(数据== 未定义){ alert(" ); } 其他 { alert(" + data.d); } } });
.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#代码



C# Code

[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){}
 }




请为我提供宝贵的知识.
谢谢.




Please guide me with you valuable knowledge.
Thanks.


您使用的是哪种网络服务器? IIS6/7/8?卡西尼号,IIS Express?
您的警报被解雇了吗? (alert(''错误:465/468''))?
我几乎可以确定这是服务器端错误(不是javascript错误),您可以在javascript中发布很多内容(远远超过17KB).

您正在发布HTML.这是IIS中可能存在的安全问题.由于验证/安全性,IIS将不接受发布到服务器的html.
您的服务器将返回潜在危险Request.Form值从客户端检测到".

添加
what kind of webserver are you using? IIS6/7/8? Cassini, IIS Express?
You''r alerts gets fired? (alert(''Error : 465/468''))?
I''m almost sure this is a server-side error (not javascript-error) You can post a lot (much more than 17KB) in javascript with no problem.

You are posting HTML. That is a possible security issue in IIS. IIS will not accept html posted to server due to validation/security.
Your server will return "Potentially Dangerous Request.Form Value Was Detected From The Client".

Add
<pages validaterequest="false" />


到您的web.config.

并且(如果您使用的是.net 4)


to your web.config.

And (if you are using .net 4)

<httpruntime requestvalidationmode="2.0" />




您的服务器返回某种错误消息.您可以使用IE Developer工具(F12)查看服务器正在响应的内容,也可以使用Chrome Dev Tools(也可以使用f12)查看服务器发送和接收的内容.




Your server is returning some sort of error-message. You can use IE Developer tools (F12) to see what the server is responding, or Chrome Dev Tools (also f12) to see what is sent to and received from server.


最后,使用在年长者的帮助下,我发现了我的代码中缺少的地方.

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

当我在Java脚本中使用escape()函数对其进行编码时,它可以正常工作.数据正在提交到数据库.

Finally with the help of my seniors I found where I was lacking in my code.

As I am passing html tags as a data parameter to my server side method written in C# from jQuery.ajax(); I need to encode the data.

As I used the escape() function in java script to encode it worked. Data is submitting to database.

var encodedHTML = escape(Html);         //here encoding Html.


这篇关于使用jquery.ajax传递大数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆