最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send POST data with AJAX? And how to get POST data in Web API? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Mar 15, 2016 at 10:42 user3378165 asked Mar 15, 2016 at 8:46 user3378165user3378165 6,90618 gold badges65 silver badges108 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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

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)
发布评论

评论列表(0)

  1. 暂无评论