jQuery AJAX post to MVC Controller object — request shows up null

后端 未结 1 390
一整个雨季
一整个雨季 2020-12-30 10:38

I know I\'m missing something in the details here.

Problem

Despite Googling, trying examples, different formats, etc, the AJAX request that I send always

相关标签:
1条回答
  • 2020-12-30 11:24

    When you JSON.stringifyied your data object you converted it to JSON. But you forgot to set the Content-Type request header and the Web API has no way of knowing whether you are sending JSON, XML or something else:

    $.ajax({
        url: '/api/contactus/newmessage',
        type: 'POST',
        contentType: 'application/json',
        done: submissionSucceeded,
        fail: submissionFailed,
        data: dataObject
    });
    

    Also when building the JSON you don't need to wrap it in an additional property that matches your method argument name. The following should work as well:

    var dataObject = JSON.stringify({
        'Email': $('#inpEmail').val(),
        'Name': $('#inpName').val(),
        'PhoneNumber': $('#inpPhone').val(),
        'Message': $('#inpMessage').val()
    });
    
    0 讨论(0)
提交回复
热议问题