Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$.ajax({}); not working for type : POST in asp.net. When posted the json data and tried to retrieve it, it returns NULL value. For example

JavaScript
   $.ajax({
                    type:"POST",
                    dataType: "json",
                    //cache: false,
                    //url: '../../../Handlers/CENTRALLOOKUP/RelationTypeHandler.ashx',
                    url: '../../../Handlers/CENTRALLOOKUP/Handler1.ashx',
                    data: { 'method': 'SaveRelationType', 'args': JSON.stringify(ko.toJS(self.Relations)) },
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        msg(result.Message);
                    },
                    error: function (err) {
                        msg(err.status + " - " + err.statusText, "FAILURE");

                    }
                });


/---------------------------------------------

In DotNet :

public void ProcessRequest(HttpContext context)
{
           context.Response.ContentType = "text/plain";
           context.Response.Write("Hello World");
          // string id = context.Request["Id"];

           if (context.Request.RequestType == "POST";)
           {
               
               string method = context.Request["method"];
               
               var arg = context.Request.Params["args"];



           }
}

In the above dot.net code, context.Request["method"] and context.Request.Params["args"] both returns NULL value.

Thanks in advance
Posted
Updated 6-Jun-14 0:03am
v2
Comments
Have you debugged?

1 solution

1) First ,you have not mentioned method name in Ajax post method
2) You have not create webmethod
3) Yu have not returned any kind of data from webmethod than how can you ger any results in Success function??

Here i have created small example of ajax method for you.. :).First of all set jQuery link to use ajax method

C#
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>


JavaScript
function GetData() {

         var MyName = "Nirav Prabtani";

         $.ajax({
             type: "POST",
             url: "PageName.aspx/MethodName",
             contentType: "application/json;charset=utf-8",
             data: "{'Name':'"+MyName+"'}" //Parameters To Pass
             dataType: "json",
             success: function (data) {

               alert(data.d); //You will get Nirav Prabtani in Alert

             },
             error: function (result) {

               alert("error");
             }
         });

     }


c#
C#
[WebMethod]
      public static string MethodName(string Name)
      {
          return Name;
      }
 
Share this answer
 
v2
Comments
rAZHere 8-Jun-14 1:56am    
Thanks for the reply . But i am calling the method in Handler file from jQuery. It works well for type= "GET" but in case of type = "POST" it returns NULL value.

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