Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda