title: jQuery/JSON Error "SyntaxError: JSON.parse: unexpected character"
body: <p>I am working on MVC4 and trying to pass values form view to controller using JQuery and JSON. The query is extracting values of checkboxes inside a grid. Following is the code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function DeleteCustomer() {
      var temp = "";
      var id = "";
  
      if (confirm("Are you sure to delete records?")) {
          $('#myGrid table tr').each(function () {
              if ($(this).find("input[id*='assignChkBx']").length &gt; 0) {
                  if ($(this).find("input[id*='assignChkBx']")[0].checked == true) {
                      temp = $(this).find("input[id*='assignChkBx']").val();
                      if (temp != "" || temp != null) {
                          id = id + " " + temp;
                          temp = "";
                      }
                  } // End of Loop
              }
          }); //End of each Loop
          $.ajax({
              url: "Customer/DeleteCustomeByID",
              type: "POST",
              contentType: 'application/json; charset=utf-8',
              dataType: "json",
              data: "{'CustomerID':'" + id + "'}",
              success: function (data) {
                  //alert('Records deleted');
                  $('#lblMessage').html('Records Deleted');
              },
              error: function (xhr, textStatus, err) {
                  alert('Error: ' + err);
                  //$('#lblMessage').html(err);
              }
          });
      }
  }
  </code></pre>
  
  <p></p>
  
  <p>My html code is following:</p>
  
  <pre><code>&lt;input type="button" id="btnDelete" value="Delete" title="Delete" onclick="DeleteCustomer()" style="color: Gray" /&gt;
  </code></pre>
  
  <p><br />
  </p>
  
  <pre><code>@{
      WebGrid grid = new WebGrid(Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
  }
  @grid.GetHtml(
      fillEmptyRows: false,
      alternatingRowStyle: "alternate-row",
      headerStyle: "grid-header",
      footerStyle: "grid-footer",
      mode: WebGridPagerModes.All,
      firstText: "&lt;&lt; First",
      previousText: "&lt; Prev",
      nextText: "Next &gt;",
      lastText: "Last &gt;&gt;",
      columns: new[] {
          grid.Column("", format: @&lt;text&gt;&lt;input class="check-box" type="checkbox" id="assignChkBx" value="@item.CustomerID" /&gt;&lt;/text&gt;),
          grid.Column("CustomerID", "CustomerID", canSort: true),
          grid.Column("CompanyName", "Company Name", canSort: true),
          grid.Column("ContactName", "Contact Name", canSort: true),
          grid.Column("Address", "Address", canSort: false),
          grid.Column("City", "City", canSort: true),
          grid.Column("", 
                      header: "Actions",
                      format: @&lt;text&gt;
                      @Html.ActionLink("Edit",   "Edit",   new { id=item.CustomerID} )
                      @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID} )
                      &lt;/text&gt;
          )
  })
  </code></pre>
  
  <p>When I click on delete button the jquery mentioned above will take the selected values to the controller. The controller code is written below:</p>
  
  <pre><code>[HttpPost]
      public ActionResult DeleteCustomeByID(string CustomerID)
      {
          Customer customer = new Customer();
          try
          {
              if (ModelState.IsValid)
              {
                  string[] values = CustomerID.Split(' ');
  
                  for (int i = 1; i &lt;= values.Length - 1; i++)
                  {
                      if (values[i].ToString().Trim() != "" || values[i].ToString() != null)
                      {
                          customer = db.Customers.Find(values[i].ToString());
                          db.Customers.Remove(customer);
                          db.SaveChanges();
                      }
                  }
                  return RedirectToAction("Index");
              }
              return View(customer); // Error in Model, if any
          }
          catch (DbEntityValidationException dbEx)
          {
              foreach (var validationErrors in dbEx.EntityValidationErrors)
              {
                  foreach (var validationError in validationErrors.ValidationErrors)
                  {
                      Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                          validationErrors.Entry.Entity.GetType().FullName,
                          validationError.PropertyName,
                          validationError.ErrorMessage);
                  }
              }
  
              throw new Exception();  // You can also choose to handle the exception here...
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
      }
  </code></pre>
  
  <p>When I click on the delete button, the values go to controller and delete records. But the problem is, after deleting records when it back to controller, I am getting the following error: "<strong>SyntaxError: JSON.parse: unexpected character</strong>" for FireFox, "<strong>json parse error unrecognized token '&lt; '</strong>" for Safari and "<strong>Error: object error.</strong>"
  I search various sites and try various solutions. But nothing is working. I am using Northwind db.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Partha</p>