Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Thank you for reading!
I'm trying to send a long character string (+4000 chars) to the server using jquery's ajax methods.

For what I read, you can post using javascript/jquery as in:
JavaScript
var poststring = "long +4000 char string";
                                
var options = {};
options.url = "Handler.ashx";
options.type = "POST";
options.contentType = false;
options.processData = false;
options.data = poststring;
options.success = function(result) {
                   //to be determined                  
                                }
                                
options.error = function(err) { alert(err.statusText); };

$.ajax(options); //AND FINALLY, THE DATA IS SENT!!


How do you fetch the value at options.data using ASP.NET with C# ?
Posted

1 solution

Try like this

Html:

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.8.2.min.js"></script>

    <script type="text/javascript">
        function SendDataToServerUsingAjax() {

            var poststring = "pass your text to the server";
            var parameters = JSON.stringify({ 'parameterName': poststring });
            $.ajax({
                type: "POST",
                url: "WebForm1.aspx/ServerMethodName",
                data: parameters,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {

                    alert(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }

    </script>


</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button Text="send data to server" OnClientClick="SendDataToServerUsingAjax(); return false" runat="server" />
        </div>
    </form>
</body>
</html>



Code Behind:

C#
[System.Web.Services.WebMethod]
       [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
       public static string ServerMethodName(string parameterName)
       {

           return "success";
       }
 
Share this answer
 
v2
Comments
Homero Rivera 1-May-15 0:30am    
Elegant!
Karthik_Mahalingam 1-May-15 0:36am    
thank u :)

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