Reputation: 1310
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);
}
});
});
And here is the cs code
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static void SendMail(string name, string email, string subject, string message)
{
}
I have noticed one more thing that is sendmail
method is not showing in asmx
file when I am run that file. Surprised that why it is not coming.
Upvotes: 2
Views: 3871
Reputation: 39946
In .asmx
file, your method should not be static
if you want to use it across several pages. So just remove the static
keyword and then it should works fine:
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public void SendMail(string name, string email, string subject, string message)
{
}
Upvotes: 3