title: How to fix '415 unsupported media error' on ajax post to controller
body: <p>The instant the post to the controller happens, the screen show the 415 error...so I am thinking that even though the data goes through somehow my json structure must not be correct.</p>
  
  <p><strong>View Ajax:</strong></p>
  
  <pre><code>function SubAll() {
  var selectedValues = 
  $('#timesheet').DataTable().column(0).checkboxes.selected().toArray();
  
  var instructions = []; //create array of objects
  for (var i = 0; i &lt; selectedValues.length; i++) {
  instructions.push({ TimeId: selectedValues[i] });
  }
  
  var jsonObject = { MasterIds: instructions };
  
  $.ajax({
  url: "/Admin/ApproveAllTimesheets",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: JSON.stringify(jsonObject),
  success: function (result) {
  console.log(result);
  },
  error: function (xhr, textStatus) {
  if (xhr.status == 401) { alert("Session Expired!"); window.location = 
  "/Account"; }
  else {
  alert('Content load failed!', "info");
  }
  }
  });
  };
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public IActionResult ApproveAllTimesheets([FromBody]ValueContainer information)
  </code></pre>
  
  <p><strong>Class Objects:</strong></p>
  
  <pre><code>public class ValueContainer
  {
      public List&lt;Value&gt; MasterIds { get; set; }
  }
  
  public class Value
  {
      public Guid TimeId { get; set; }
  }
  </code></pre>
  
  <p><strong>Payload:</strong></p>
  
  <pre><code>{"MasterIds":[{"TimeId":"ad98749f-9083-464b-aac2-0d685a7de809"}]}
  </code></pre>
  
  <p><strong>UPDATE #1</strong></p>
  
  <p>As it turns out the fix is the way I was calling the function. Instead of the button onclick="SubAll()" I replaced that with a simple  and used jQuery to intercept the click event, prevent it, and then call into the function...and now no 415 error.</p>
  
  <p><strong>View Button</strong></p>
  
  <pre><code>&lt;button id="ApproveAll" class="btn btn-success"&gt;Approve All&lt;/button&gt;
  </code></pre>
  
  <p><strong>View jQuery</strong></p>
  
  <pre><code> $(document).ready(function () {
          timesheet = $('#timesheet').DataTable({
              responsive: {
                  details: {
                      renderer: function (api, rowIdx, columns) {
                          var data = $.map(columns, function (col, i) {
                              return col.hidden ?
                                  '&lt;tr data-dt-row="' + col.rowIndex + '" 
  data-dt-column="' + col.columnIndex + '"&gt;' +
                                  '&lt;td&gt;' + col.title + ':' + '&lt;/td&gt; ' +
                                  '&lt;td&gt;' + col.data + '&lt;/td&gt;' +
                                  '&lt;/tr&gt;' :
                                  '';
                          }).join('');
  
                          return data ?
                              $('&lt;table/&gt;').append(data) :
                              false;
                      }
                  }
              },
              columnDefs: [
              {
                  targets: 0,
                  orderable: false,
                  searchable: false,
                  checkboxes: true
              },
              {
                  targets: 5,
                  visible: false
              },
              {
                  targets: 6,
                  visible: false
              }
              ],
              order: [
                  [1, 'asc']
              ]
          });
  
          $('#ApproveAll').on('click',
              function (e) {
                  var selectedValues = 
  $('#timesheet').DataTable().column(0).checkboxes.selected().toArray();
                  var instructions = []; //create array of objects
                  for (var i = 0; i &lt; selectedValues.length; i++) {
                      instructions.push(selectedValues[i]);
                  }
  
                  var jsonObject = { MasterIds: instructions };
  
                  $.ajax({
                      url: "/Admin/ApproveAllTimesheets",
                      type: "POST",
                      contentType: "application/json;charset=utf-8",
                      data: JSON.stringify(jsonObject),
                      traditional: true,
                      statusCode: {
                          415: function () {
                              Response.redirect("/Admin/Index");
                          }
                      },
                      success: function (result) {
                          console.log(result);
                      },
                      error: function (xhr, textStatus, errorThrown) {
                          if (xhr.status == 401) { alert("Session 
  Expired!"); window.location = "/Account"; }
                          else {
                              alert('Content load failed!', "info");
                          }
                      }
                  });
                  e.preventDefault();
              });
      });
  </code></pre>