0

I have the following JQuery:

$(document).ready(function () {
  $("#send").on("click", function () {
    $.ajax({
      url: 'http://localhost:3484/api/mail/send/',
      type: 'POST',
      dataType: 'text',
      data: '[email protected]'
    });
  });
});

And the Web API action is:

[Route("api/mail/send"), HttpPost]
public void Send([FromBody]String email) {
}

The action is called but the email is null.

What am I missing?

1
  • But i ser the data type as text. Wouldn't that be enough? Commented Oct 6, 2014 at 23:04

2 Answers 2

1

This should work

$(document).ready(function () {

  $("#send").on("click", function () {
    var data={email :'[email protected]'};

    $.ajax({
      url: 'http://localhost:3484/api/mail/send/',
      type: 'POST',
      contentType : 'application/json',
      data:  JSON.stringify(data)
    });

  });

});
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function () {
  $("#send").on("click", function () {
    $.ajax({
      url: 'http://localhost:3484/api/mail/send/',
      type: 'POST',
      dataType: 'text',
      data: { email: `[email protected]' },
    });
  });
});

data must be JSON object. Alternative for $.ajax is $.post. It has documentation here: http://api.jquery.com/jquery.post/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.