title: AJAX Query Doesn't Call REST API Controller Method
body: <p>I have a REST API controller configured as follows. On button click "PlayersRegistration", the update function in javascript is called prorperly. But the problem is that the PostPlayers Method in the DefaultController class never gets called using success: function(data) statement in update function. Instead when I check on the console in the browser by inspecting elements, i see Internal Server Error 500. I don't know what's wrong.  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;   
  using System.Net;
  using System.Net.Http;  
  using System.Web.Http;
  
  namespace WebApplication2.Models
  {
    public class Default : ApiController
    {
      public string regID { get; set; }
      public string firstName { get; set; }
      public string lastName { get; set; }
      public string teamName { get; set; }
      public DateTime dateOfBirth { get; set; }
  
    }
  }
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using WebApplication2.Models;
  using System.IO;
  using System.Globalization;
  using System.Windows.Forms;
  namespace WebApplication2.Controllers
  {
   public class DefaultController : ApiController
   {
      List&lt;Default&gt;players = new List&lt;Default&gt;();
      string path = Path.Combine(Directory.GetCurrentDirectory(), "\\players.txt");
  
      public void readFile() 
      {
          string line;
          StreamReader file = new StreamReader(path);
          string[] data;
          players.Clear();
  
          while ((line = file.ReadLine()) != null)
          {
              data = line.Split(',');
              Default player = new Default();
              player.regID = data[0];
              player.firstName = data[1];
              player.lastName = data[2];
              player.teamName = data[3];
              player.dateOfBirth = DateTime.ParseExact(data[4],
                  "yyyy-mm-dd", CultureInfo.InvariantCulture);
              players.Add(player);
          }
          file.Close();
      }
  
      public IHttpActionResult PostPlayers(Default playerInfo)
      {
          readFile();
          MessageBox.Show("5");
          var player = players.FirstOrDefault((p) =&gt; p.regID == playerInfo.regID);
          if (player != null)
          {
              players.Remove(player);
              players.Add(playerInfo);
          }
  
          else
          {
              players.Add(playerInfo);
          }
  
          using (StreamWriter sw = File.CreateText(path))
          {
              for (int i = 0; i &lt; players.Count(); i++)
                  sw.WriteLine(Players_Display(players[i]), Environment.NewLine);
              sw.Close();
          }
  
          return Ok(players);
      }
  
      public string Players_Display(Default player)
      {
          string data = player.regID + "," + player.firstName + "," + player.lastName + "," + player.teamName + "," + player.dateOfBirth.ToString("yyyy-mm-dd").Substring(0,10);
          return data;
      }
  
     }
  }
  </code></pre>
  
  <p>THE HTML CODE is as follows</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;div&gt;
      &lt;h2&gt; All Players&lt;/h2&gt;
      &lt;ul id="players"&gt;&lt;/ul&gt;
  
   &lt;/div&gt;
  
   &lt;div&gt;
      &lt;br /&gt;
      &lt;h2&gt; Search or Delete &lt;/h2&gt;
      &lt;select id="options_for_search_delete"&gt;
          &lt;option value="id"&gt;ID&lt;/option&gt;
          &lt;option value="name"&gt;Name&lt;/option&gt;
      &lt;/select&gt;
      &lt;input type="text" id="data" size="5" /&gt;
      &lt;input type="button" value="Search" onclick="search()" /&gt;
      &lt;input type="button" value="Delete" onclick="delet()" /&gt;
      &lt;br /&gt;
      &lt;p id="result_players" /&gt;&lt;br&gt;
      &lt;h2&gt;Player Registration&lt;/h2&gt;
      &lt;br /&gt;
      &lt;form&gt;
          &lt;label for="id"&gt; RegistrationID:&lt;/label&gt;&lt;br&gt;
          &lt;input type="text" id="regID" name="regID"&gt;
          &lt;br /&gt;
          &lt;label for="firstName"&gt;First Name:&lt;/label&gt;&lt;br&gt;
          &lt;input type="text" id="firstName" name="firstName"&gt;
          &lt;br /&gt;
          &lt;label for="lastName"&gt;Last Name:&lt;/label&gt;&lt;br&gt;
          &lt;input type="text" id="lastName" name="lastName"&gt;
          &lt;br /&gt;
          &lt;label for="teamName"&gt;Team Name:&lt;/label&gt;&lt;br&gt;
          &lt;input type="text" id="teamName" name="teamName"&gt;
          &lt;br /&gt;
          &lt;label for="dateofbirth"&gt;Date Of Birth:&lt;/label&gt;&lt;br&gt;
          &lt;input type="text" id="dateOfBirth" name="dateOfBirth"&gt;
          &lt;br /&gt;
          &lt;input type="button" id="post" value="Player Registration" onclick="update();" /&gt;
          &lt;p id="data_validation"&gt;&lt;/p&gt;
      &lt;/form&gt;
  
   &lt;/div&gt;
  
   &lt;script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"&gt;&lt;/script&gt;
   &lt;script&gt;
  
      var uri = 'api/Default';
  
      $(document).ready(function () {
  
          $.ajax({
              type: 'GET',
              url: uri,
              success: function (data) {
                  displayData(data);
              }
          })
      })
  
      function format(item) {
          return item.regID + "," + item.firstName + " " + item.lastName + "," + item.teamName + "," + item.dateOfBirth.toString().substring(0, 10);
      }
  
      function displayData(data) {
          $('ul').empty(); 
          $.each(data, function (key, item) {
              $('&lt;li&gt;', { text: format(item) }).appendTo($('#players'));
          });
      }
  
  
  
      function update() {
  
          $('#result_players').text("");
          $('#data_validation').text("");
          if ($('#regID').val() == "" || $('#firstName').val() == "" || $('#lastName').val() == "" || $('#teamName').val() == "" || $('#dateofbirth').val() == "") {
              $('#data_validation').text("Input Boxes Cannot be Empty");
          }
          else {
              $.ajax({
                  type: 'POST',
                  url: uri,
                  data: String($('form').serialize()),
                  success: function (data) {
                      displayData(data);
                  },
                  error: function (jqXHR, textStatus, err) {
                      alert(err);
                  }
              })
          }
      }
    &lt;/script&gt;
   &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>