Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Ajax jQuery
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# 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.
Posted 23-Jul-12 2:51am
Comments
AlluvialDeposit at 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 Namdev at 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... :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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" />
to your web.config.
 
And (if you are using .net 4)
<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.
  Permalink  
Comments
Manish Namdev at 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.
AlluvialDeposit at 23-Jul-12 10:20am
   
Did you try to use browsers developr tools to see the response from server?
Manish Namdev at 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.
AlluvialDeposit at 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 Namdev at 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.
AlluvialDeposit at 24-Jul-12 4:39am
   
Tha request never reaches your method. The servers configuration is stopping the request before it actually hits your method. Add a protected void Application_Error(object sender, EventArgs e) in your global.asax and put your breakpoint in that method. Or log all exceptions catched in global.asax
 
protected void Application_Error(object sender, EventArgs e)
{
logExceptionDetails(Server.GetLastError());
}
 
Manish Namdev at 24-Jul-12 6:22am
   
Many Thanks for every one and specially to AlluvialDeposit to keep alive this thread and helping me and putting his best effort with me.
 
Discussing with one of my senior in programming, he suggested me to encode via escape() method.
 
And it's working now I'm able to send large data via jquery.ajax() and the execution is also reaching to server side...
 
Many Thanks once again :) happy coding
Manish Namdev at 24-Jul-12 6:23am
   
I will try this method also... to check how to trace the errors.I never used this method.
Espen Harlinn at 24-Jul-12 4:36am
   
Nice effort :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 195
3 DamithSL 195
4 Richard MacCutchan 178
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 24 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100