2

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.

3
  • Before dealing with error, So simply posting to this url sends emails? User must not have direct access to this Commented Oct 1, 2017 at 6:53
  • In asmx file also that sendmail method is not displaying Commented Oct 1, 2017 at 6:55
  • Yup you are absolutely right. Got IT. Thank You @S.Akbari Commented Oct 1, 2017 at 7:00

1 Answer 1

3

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)
{
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.