C# Web Method Is Not Calling in JavaScript

C# Web method is not calling in javascript

There are few rules for ajax to work with asp.net.

  • Your WebMethod should be public and static.
  • If your WebMethod expects some parameter(s) than these parameter(s) must be passed as data in ajax.
  • Name of parameter(s) should be same in WebMethod and in data part of ajax.
  • Data passed from ajax should be in json string.For this you can use JSON.stringify or you will have to surround the values of parameter(s) in quotes.

Please check the below sample ajax call

function CallAjax()
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/CallAjax",
data: JSON.stringify({ name: "Mairaj", value: "12" }),
dataType: "json",
async: false,
success: function (data) {
//your code

},
error: function (err) {
alert(err.responseText);
}

});
}

[WebMethod]
public static List<string> CallAjax(string name,int value)
{
List<string> list = new List<string>();
try
{
list.Add("Mairaj");
list.Add("Ahmad");
list.Add("Minhas");
}

catch (Exception ex)
{

}

return list;
}

EDIT

If you use GET in ajax than you need to enable your webmethod to be called from GET request. Add [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] on top of WebMetod

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static int ItemCount()

.NET Web Method not being called from javascript only Page

Can you try with the below code , make sure the path to the method is correct in the url below. also enable page methods at the scriptmanager level if you are using one.

   $.ajax({
url: '/adm/clientAccess.aspx/MyMethod',
data: {},
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
success: function (data) {
console.log(data);
//alert(1);
},
error: function (response) {
alert(response.responseText);
}
}
);

asp.net Web Method not working

Try this:

[WebMethod(EnableSession=true)]

Otherwise your method won't have access to the current SessionState, and the part where you try to save data to Session won't work.

The first example where you call PageMethods.GirisKontrol is unclear - your JavaScript isn't going to know about the server-side methods.

The second example using Ajax looks closer, but you're not passing any data.

data: "{}",

perhaps you mean this:

data: "{'UserName': 'asd', 'Pass': 'sad'}",

server side c# method is not calling from ajax - asp.net

Adding
"contentType: "application/json; charset=utf-8"," in jquery called the server side method.

Thanks everyone for help. :)

Not able to call a webmethod in a code page from master page

Make your method static

[WebMethod]
public static string heelo(int roll, string name)
{

return "Mubashir";
}

To know more about Why do ASP.Net AJAX PageMethods have to be static read this.

Ajax call not hitting the webmethod in C# Code Behind

I think the problem is that you are formatting to JSON only parameter value. Instead of var jsonFormValues = JSON.stringify(formValues);, you need to use JSON.stringify for parameter itself too:

    $.ajax({
type:POST,
async: false,
url: "RegistryOpt.aspx/SendOpt",
data: JSON.stringify({ jsonFormValues: formValues }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
onWebMethodSucceeded();
}
});

Call WebMethod in C# codebehind without using a server form in ASPX page

You need to declare the method as static.

[WebMethod]
public static string CreateJob()
^^^^^
{
//rest of my database code here
}

Another issue is if input[type=submit] is ASP.Net Button control, it will post back to server. You cannot use ASP.Net Server control to make jQuery Ajax call - $.ajax.

// This code won't work if `input[type=submit]` is a server button control
$(function () {
$("input[type=submit]").click(function () {
handleClick();
createJob();
});
});

you need to use regular html input or button control with type=button instead of type=submit.

C# [WebMethod] is not hitting when Content-Type ''application/Json is not provided

I further investigated my question and tried to find other ways around, I came up with a solution. I created a new WebForm named GetDocumentHtml.aspx and in its Page_Load Method i am doing operation which i was previously doing in GetPageHtml WebMethod.

Now in iframe ng-src i am providing this URL

/GetDocumentHtml.aspx?file=calibre.docx&page=1

Instead of

default.aspx/GetPageHtml?file=candy.pdf&page=1

In Page_Load Method i am using following code to get URL parameters

var file = GetValueFromQueryString("file");
var page =Convert.ToInt32(GetValueFromQueryString("page"));

GetValueFromQueryString Method

 private String GetValueFromQueryString(String value)
{
try
{
return Request.QueryString[value].ToString();

}
catch (System.Exception exp)
{
return String.Empty;
}
}

I hope this information will be helpful for someone. Thanks.



Related Topics



Leave a reply



Submit