3

I have seen quite a few posts online regarding this kind of problem and tried different approach, e.g. JSON.stringify the parameter, but none of them works on mine case.

I thought it should be a really simple and straight forward coding experience. But couldn't figure out what I did wrong.

Here is my JQuery code:

$(document).ready(function () {
    $('#SendEmails').click(function () {
        var emails = $("#EmailList").val();     

        $.ajax({
            url: '/Requests/SendEmails',
            type: "POST",
            data: { 'emails': emails },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            },
            failure: function (response) {
                alert(response.responseText);
            }
        })
    })
})

And my action method is like:

[HttpPost]
public string SendEmails(string emails)
{
    return "Good";
}

I always get null in action method when I debug the code.

But if I change the url to this:

url: '/Requests/SendEmails?emails=' + emails,

and remove

data: { 'emails': emails },

it will work.

Anyone could point me what is wrong with the original code? I don't think .Net Core 2.x should make any difference right?

Thank you.

3
  • type: "POST" should be method: "POST". Correct that and see if it works. Commented Feb 27, 2018 at 16:33
  • 2
    @Spokey type is an alias for method Commented Feb 27, 2018 at 16:36
  • Remove contentType: "application/json; charset=utf-8", (your not stringfying the data) Commented Feb 27, 2018 at 21:48

2 Answers 2

10

Finally, after tried many combinations I found below code works after changed:

  1. make variable JSON.stringify
  2. add [FromBody] in action method

Thanks for Arunraja's tips, [FromBody] is a must to have to read string type parameter from body.

$(document).ready(function () {
    $('#SendEmails').click(function () {
        var emails = $("#EmailList").val();     

        $.ajax({
            url: '/Requests/SendEmails',
            type: "POST",
            data: JSON.stringify(emails),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            },
            failure: function (response) {
                alert(response.responseText);
            }
        })
    })
})

[HttpPost]
public string SendEmails([FromBody]string emails)
{
    return "Good";
}
Sign up to request clarification or add additional context in comments.

Comments

4

To pass primitive type in the body, then you have to add [FromBody] in front of your primitive type parameter in your WebAPI controller method.

[HttpPost]
public string SendEmails([FromBody]string emails)
{
    return "Good";
}

1 Comment

Tried it, doesn't seem make any difference, it is still null in the action method.

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.