user9677867
user9677867

Reputation:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data, why?

I am using this to get and fill table but getting an error:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I have tried JSON.parse and everything but still not working.

  $(document).ready(function () {
      var url = '@Html.Action("GetServices")';
      var data= ''

      $.get(url, data, function (response) {
          $("#tblServices").html("");
          $.each(response, function (i, val) {
              $("#tblServices")
                  .append($('<tr>').append($('<td>')
                      .html(response.ServiceID))
                  .append($('<td>').html(response.ServiceName))
                  .append($('<td>').html(response.ServicePrice)));
          });
      });

Upvotes: 2

Views: 733

Answers (1)

Niladri
Niladri

Reputation: 5962

One of the main reason for this can be the use of @HTML.Action here in this line below -

var url = '@Html.Action("GetServices")';

as @HTML.Action calls child action in a controller and return Html string as result. But as you are expecting a JSON data in the response it results in an error. It should be @Url.Action() instead which returns a URL to be called through $get method.

Upvotes: 1

Related Questions