Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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:
JavaScript
$.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# Code
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){}
 }



Please guide me with you valuable knowledge.
Thanks.
Posted
Comments
StianSandberg 23-Jul-12 8:03am    
Are u sure the serverside script is not executed?
in your mySavePost() try to log exceptions to see if it fails
catch(Exception ex){
System.IO.File.WriteAllText("c:\\Logs\\error.log", ex.ToString());
}
Database datatypes big enough?
There are limits in webserviers, but not event close to 17KB. I think default in IIS is 4MB, so i don't think that's the issue here.
Manish Kumar Namdev 23-Jul-12 8:36am    
Thanks to join this thread.
Yes the serverside script is not executed... If I pass large amount of data. With less amount of data it works and insert the data in the table. I tried to log exception to see but the execution doesn't reach to server side in debug mode... :(

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
XML
<pages validaterequest="false" />

to your web.config.

And (if you are using .net 4)
XML
<httpruntime requestvalidationmode="2.0" />



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.
 
Share this answer
 
Comments
Manish Kumar Namdev 23-Jul-12 10:08am    
Dear AlluvialDeoposit the statement alert("Error : 465"); is actually the line no. where the error occurred in my javascript this was for my own reference to know the location which function creating the error.
pardon me if this increase the confusion.

I'm using IIS 7. Actually I have a req. to submit html tags in my database...
To stop "Potentially Dangerous Request.Form Value Was Detected From The Client". error message I used jQuery.ajax because it silently send the data to server side without refresh.
StianSandberg 23-Jul-12 10:20am    
Did you try to use browsers developr tools to see the response from server?
Manish Kumar Namdev 24-Jul-12 0:58am    
I debug my jQuery.ajax() using IE developer tools. Found the following message from response object.

1. status = 500;
2. Status Text = Internal Server error
3. ResponseHeader = "Content-Type: application/json; charset=utf-
8\r\nServer: Microsoft-IIS/7.0\r\njsonerror:
true\r\nX-Powered-By: ASP.NET\r\nDate: Tue, 24 Jul
2012 04:30:09 GMT\r\nContent-Length: 91\r\n\r\n";
4. Response - Text = "{\"Message\":\"There was an error processing the
request.\",\"StackTrace\":\"\",\"ExceptionType\":\"\"}";

I also tried to use Fiddler2 to debug But I couldn't understand that.
StianSandberg 24-Jul-12 2:36am    
perfect! There you have the whole exception (from the server) So the javascript is posting data. If you do this in debug mode you should be able to get some more exception details. Or you could catch all exceptions in global asax so you can catch exceptions like this)
Manish Kumar Namdev 24-Jul-12 3:33am    
I have put the break point inside the code behind as I'm using c#. But execution doesn't go there when jQuery.ajax() comes with large data. In case of less data execution reach to code behind and data inserted to table.
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.

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);
                    }
                }
            });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900