How to use jQuery ajax with asp.net user controls?

I want to use ajax with asp.net user control,

$.ajax({ type: "POST", url: "*TCSection.ascx/InsertTCSection", data: "{id:'" + id + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { var URL = msg.d; alert(URL); }); 

.cs code

 [WebMethod] public static string InsertTCSection(string id) { string result = "deneme"; return result; } 
+6
source share
4 answers

You cannot call a method stored inside usercontrol via jquery. because .ascx controls are not a real url.

They are intended to be embedded in some ASP.NET pages, so they do not exist at run time. what you can do is create a separate service and post your method there. e.g. webservices.

see this , this and many others

+5
source

I use a common handler to solve this problem.

+2
source

Try:

  $.ajax({ type: "POST", url: "*TCSection.ascx/InsertTCSection", data: JSON2.stringify({ id: id}, //Your 2nd id is passing value but i dont know where its come from contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { var URL = msg.d; alert(URL); } )}; 

and in cs:

 [WebMethod] public static string InsertTCSection(string id) { string result="deneme"; return result; } 
+1
source

I think you might miss this attribute

  [System.Web.Script.Services.ScriptService] 

Before you execute a class class

 [System.Web.Script.Services.ScriptService] public class TCSection: System.Web.Services.WebService { [WebMethod] public static string InsertTCSection(string id) { } } 

Or there may be another reason that the path to the web service is incorrect.

+1
source

All Articles