views:

1324

answers:

3

I would (as the question states) like to make an asynchronous call, preferably using ASP.net AJAX.

The code for the WebMethod looks like this:

[WebMethod]
public void SendMail(string name, string email, string subject, string body)
{
  MailMessage toSend = new MailMessage(email, [email protected], subject, body);
  var smtp = new SmtpClient();
  smtp.Send(toSend);
}

The fields on the view are, not surprisingly: name, email, subject, body.

In my attempts to do this I haven't been able to get to the WebMethod. The service reference is in place, so at least I didn't screw that up.

Thanks for the help...

+3  A: 

This is not an answer to your question but a warning. I was looking at this method and thinking, "hmm, I wonder if ASP.NET cares if a call to this web method comes from your site or somewhere else?" A quick google search leads me to believe that there isn't any checking to make sure some douche isn't using your web methods for his own malicious desires (here's a blog post talking about this).

So, before you get this working, you might want to think about ways to prevent someone from hijacking your webmethod to send me Viagra emails. Because if I get a Viagra email from your website, I'm not going to be very happy with you.

Will
Hmmm, I hadn't considered that, thanks for the heads up. Do you have any suggestions?
KevDog
I took a closer look at the link you put up. The call is not in the code behind for the page, but is in a .asmx file for a separate web service. I'll do some more reading to see if the same caveats apply.
KevDog
A: 

Here you can find an example of invoking asynchronous methods with AJAX in ASP.NET MVC with elements like

<% using (Ajax.Form("SendMail", new AjaxOptions { UpdateTargetId = "resultDiv" })) { %>

   <!-- Your form elements here... -->

<% } %>

You can receive the params in the controller method and call the webservice from there.

mapache
A: 

According to the MSDN Library

In order for a Web service to be accessed from script, it must be an .asmx Web service whose Web service class is qualified with the ScriptServiceAttribute attribute. Individual methods to be called from script must be qualified with the WebMethodAttribute attribute.

see http://msdn.microsoft.com/en-ca/library/bb398998.aspx

Matthew
Well, the WebMethod attribute is right there and the scriptservice attribute is part of the class. So I'm not sure what you are trying to get to with this answer.
KevDog