4

I am trying to use JQuery ajax post to call an ASP.Net Core Controller. I have tried all different scenarios, but any of them is not working. Here is my Javascript Function:

   $("#verify-answer").click(function () {

        if ($("#question_answer").val() != "") {
            var request = JSON.stringify({
                'Answer': $('#question_answer').val(),
                'UserId': 1,
                'TenantId': 0
            });

            $.ajax({
                url: "/Account/PasswordRecoveryChallengResponseGetToken",
                type: "POST",             
                contentType: 'application/json',
                data: request,
                datatype: "json",
                success: function (result) {

                    console.log("answer token challenge: ");

                },
                error: function (result) {                      
                }
            });
        }

        return false;
    });

Partial View HTML:

 <div>
     <form>
         <p>Challenge-Response Option</p>
          <label for="username">Username</label>
          <input type="text" class="form-control" id="username_recovery">
          <button type="submit"  id="get-questions" onclick="return false;" >Retrieve Question</button>
      </form>
  </div>


 <div id="question">
    <div>                      
       <form>
            <div id="user_question"> </div>
            <label for="username">Answer</label>
            <input type="text" id="question_answer">
            <div  id="questions_answer_message"></div>
            <button type="submit"  id="verify-answer">Verify Answer</button>
        </form>
     /div>
  </div>

This is my controller:

[HttpPost]
public async Task<IActionResult> PasswordRecoveryChallengResponseGetToken(ResetPasswordQuesionChallengeRequest request)
{

}

Model passed:

public class ResetPasswordQuesionChallengeRequest : Core.TenantClientEntityBase
{

    [Required]
    public string Answer { get; set; }
}


 public abstract class TenantClientEntityBase
{
    [Required]
    public int TenantId { get; set; }

    [Required]
    public int UserId { get; set; }
}

This is Request Payload:

enter image description here

The request parameter has always the following values:

  • Answer = null
  • UserId = 0
  • TenantId = 0

What am I doing wrong?

2
  • Could you check the network tab of developer tools (F12) and verify the request payload is as expected? Commented Dec 5, 2016 at 16:17
  • This is the request payload: {Answer: "test", UserId: 1, TenantId: 0} Commented Dec 5, 2016 at 16:22

1 Answer 1

3

I find I have to put [FromBody] in all my POSTs or I encounter the same issue.

e.g.

public async Task<IActionResult> PasswordRecoveryChallengResponseGetToken([FromBody] ResetPasswordQuesionChallengeRequest request)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Do you know the reason behind that?
It can be useful when you get the data from multiple sources e.g.
e.g. Update(Guid id, [FromBody] UpdateData data), but I'm not sure why it doesn't assume FromBody by default on POSTs...
@holland1991: One of the reasons may be that WebAPI and MVC got merged into one single framework. Before, MVC and WebApi would be inherited from different base classes. Now that there is only one class (or none at all), there are two major ways to pass a model to the controller: json/xml (typical for WebApi) in the body or a form request (typical for MVC)

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.