c# - Codebehind function not being called by Ajax POST -
my ajax post not running code behind method , not returning data.
<%@ page language="c#" autoeventwireup="true" codebehind="test.aspx.cs" inherits="asptest.test" %>
test.aspx (ajax script)
<script> $(".avatarthumb").click(function () { $.ajax({ type: "post", url: "test.aspx/mymethod", //data: {"s": "some data passed through" }, //contenttype: 'application/json; charset=utf-8', //datatype: 'json', success: function (response) { alert(response.d); //returns "undefined" }, failure: function(response) { alert(response); } }); }); </script>
removing contenttype , datatype reach success not run codebehind method. contenttype and/or datatype active not reach success nor failure. ff firebug not display errors.
*test.aspx.cs codebehind method
[system.web.services.webmethod] public static string mymethod() { (*) debug.writeline("mymethod called!"); // (*) breakpoint never reached return "method called!"; }
change code this. should work fine
var senddata = {}; $.ajax({ type: "post", url: "test.aspx/mymethod", data: json.stringify(senddata), contenttype: "application/json; charset=utf-8", datatype: "json", success: function (data) { alert(data.d); }, error: function (result) { console.warn(result.statustext); } });
your c# method :
[system.web.services.webmethod] [scriptmethod(responseformat = responseformat.json)] public static string mymethod() { return "method called!"; }
the above code works if ur page test.aspx @ root level of project. if not change url parameter value "../test.aspx/mymethod" based on location depth of file.
Comments
Post a Comment