Jacky
Jacky

Reputation: 298

Uncaught SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (Asp.net MVC)

I am developing a Website using MVC Application. I have used Ajax to delete the particular record. The thing here is, the delete function is working fine using locally.

Few days back, i deployed the MVC application to the GoDaddy server. When i test the application there, i see the delete function is not working properly using Ajax.

Screenshot of the Error

Here is the script. When i debug, i can see the id value.

<script>
function DeleteCurrency(CourseId) 
{
    $.ajax({
        url: "@Url.Action("Delete","Course")",
        dataType: "json",
        type: 'DELETE',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ 'id' : CourseId }),
        async: true,
        processData: false,
        cache: false,
        success: function (data) {
            deleteCourse(CourseId);
        },
        error: function (xhr) {
            alert('error');
        }
    });
}

function deleteCourse(row_id)
{
    $("#" + row_id).remove();
    toastr.error('Yes! You have successfully deleted!')
}

And the controller action code to delete.

[Authorize]
    [HttpDelete]
    public ActionResult Delete(int id)
    {
        try
        {
            CourseBl.Delete(id);
            return Json("Deleted Successfully");
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

Can anyone help me with this issue?

Upvotes: 0

Views: 1596

Answers (1)

Elyas Esna
Elyas Esna

Reputation: 635

change your ajax call to

$.ajax({
    url: "@Url.Action("Delete","Course")?id=" + CourseId,
    dataType: "json",
    type: 'DELETE',
    dataType: 'json',
    cache: false,
    success: function (data) {
        deleteCourse(CourseId);
    },
    error: function (xhr) {
        alert('error');
    }
});

and it should works. if it doesn't, try this:

    $.ajax({
    url: "@Url.Action("Delete", "Course")",
    type: 'Post',
    data: {
      id: CourseId,
    },
    cache: false,
    dataType: 'json',
    success: function (data) {
      deleteCourse(CourseId);
    },
    error: function (jqXhr, textStatus, errorThrown) {
      alert('error');
    },
});

and at last change your action method to [HttpPost]

Upvotes: 1

Related Questions