Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send POST data with AJAX? And how to get POST data in Web API?

On my MVC project I have to send a HTML table as an email to the client.

I have an Email Function on a Web API that I should call:

public class FunctionsController : ApiController
{
    [HttpPost]
    [Route("{controller}/email")]
    public void SendEmail(string to, string from, string body, string subject, string bcc)
    {
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = "xxxx";
        MailMessage mailMessage = new MailMessage();

        mailMessage.IsBodyHtml = true;
        mailMessage.Body = body;
        mailMessage.Subject = subject;
        mailMessage.From = new MailAddress(from);
        mailMessage.To.Add(new MailAddress(to));
        mailMessage.Bcc.Add(new MailAddress(bcc));

        smtpClient.Send(mailMessage);
    }

I'm not sure how to do it.

This is my sendEmail javascript function on my MVC project (using Knockout):

   self.sendEmail = function (to, from, body, subject, bcc) {
    $.ajax({
        url: "../API/functions/email",
        type: "POST",
        data: {
            'to': to,
            'from': from,
            'body': body,
            'subject': subject,
            'bcc' : bcc
        },
        contentType: "application/json",
        success: function (data) {
           console.log(ko.toJSON(data));
        }
    });
}

How should I get the POST data in the Web API? Is my javascript function correct?

Thanks in advance.

like image 943
user3378165 Avatar asked Dec 11 '22 17:12

user3378165


2 Answers

It's always better practice to make a model/viewmodel.

public class EmailViewModel
{
    public string Body { get; set; }
    public string Subject { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public string Bcc { get; set; }
}

Now the controller method will be like:

 public void SendEmail(EmailViewModel email)
  {
       .......
  }

and the ajax call will be like:

 var email={
        'Body': body,
        'Subject': subject ,
        'From': from,
        'To': to,
        'Bcc': ''
   };
   $.ajax({
    url: "../xxx/functions/email/",
    type: "POST",
    data: email,
    contentType: "application/json",
    success: function (data) {
        callback(data);
    }
});

Hope this will help :)

like image 136
Mahbubur Rahman Avatar answered Apr 29 '23 01:04

Mahbubur Rahman


Your data fields in js:

    data: {
        'from_email': from,
        'to': to,
        'autotext': 'true',
        'subject': subject,
        'html': body
    },

Should match your controllers parameters names:

public void SendEmail(string sBody, string sSubject, string sFrom, string sTo, string sBcc)

If the are match MVC will automatically bind them.

So if you want to get your data in controller you should either change your js of controller signature.

For example let's change controller:

public void SendEmail(string html, string subject, string from_email, string to, string sBcc)
like image 20
teo van kot Avatar answered Apr 29 '23 01:04

teo van kot