Interviews - C# Corner

Nitin Choudhary
Can we call C# code behind using jQuery?
By Nitin Choudhary in JQuery onJan 20 2015
  • Md. Raskinur Rashid
    Jan, 2015 28

    Yes we can call C# Web method from Jquery. Like - $.ajax({type: "POST",url: "Default.aspx/WebMethodName",data: '{}',contentType: "application/json",dataType: "json",success: function (data) {},error: function (x, e) {alert("The call to the server side failed. " + x.responseText);}})

    • 3
  • Bidyasagar Mishra
    Jul, 2019 12

    we can but we need to specify the backend method as static .

    • 0
  • Amaya Pani
    Jun, 2015 17

    YES

    • 0
  • Vaibhav Salwe
    May, 2015 30

    Yes,But it having some prerequisiteC# method mark as webmethod. Webmethod should be public. Webmethod also be static.[webmethod] Public static YourMethodName( parameter... ) { } for your knowledge this method call in two ways one way is Using PageMethods.YourMethodName( Parameter, OnSuccess ,OnFailure ). For this way you required to add on page.Second way using Jquery Ajax.Sorry for spelling mistake.

    • 0
  • Shuvojit Halder
    May, 2015 19

    $.ajax({ type: "POST", url: "Default.aspx/WebMethodName", data: '{}', contentType: "application/json", dataType: "json", success: function (data) {}, error: function (x, e) { alert("The call to the server side failed. " + x.responseText); } })and your method should be [webmethod] public static string WebMethodName() { return "success";}

    • 0
  • Rasmita Dash
    Apr, 2015 3

    We can call a Web method using ScriptManager as well. & In javascript, PageMethods.(parameters, onSuccess, onFail);

    • 0
  • Rahul Bansal
    Mar, 2015 31

    Is it only work with web method?

    • 0
  • Abrar Ahmad Ansari
    Mar, 2015 10

    use Jquery.Ajax$.ajax({url: "test.html",context: document.body }).done(function() {$( this ).addClass( "done" ); });

    • 0
  • Navin Kumar
    Feb, 2015 27

    Yes, we can call C# web method from Jquery for Exmple JavaScript code$.ajax({type: "POST",url: "CS.aspx/GetName",data: '{name: abcd }',contentType: "application/json; charset=utf-8",dataType: "json",success: OnSuccess,failure: function(response) {alert(response.d);}});}function OnSuccess(response) {alert(response.d);}C#[System.Web.Services.WebMethod]public static string GetName(string name){return "Hello " + name ; }

    • 0
  • Nitin Choudhary
    Jan, 2015 20

    Yes, we can call C# code from jQuery as it supports .net application

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS