Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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 :)

over 4 years ago · Santiago Trujillo Report

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!