I am using web services in asp.net with c#. In that I am working in one task that is to call the method from ajax in web services. But whenever I call the method from ajax in button click event at that time it shows the error i.e. 500 (internal server error) and when I am go the network in developer tools it shows me Unknown web method and method. Parameter name : methodname.
Here is the ajax function code
$("#submit").click(function () {
$.ajax({
type: "POST",
url: "OakscrollWebService.asmx/SendMail",
dataType: "json",
data: JSON.stringify({ name: $('#name').val(), email: $('#mail').val(), subject: $('#subject').val(), message: $('#message').val() }),
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
failure: function (data) {
alert("something went wrong");
//console.log(msg);
}
});
});
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static void SendMail(string name, string email, string subject, string message)
{
}
In .asmx
file, your method should not be static if you want to use it across several pages:
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public void SendMail(string name, string email, string subject, string message)
{
}