Asp.net mvc 4 jquery ajax request return internal

2019-07-21 07:07发布

问题:

I tried to establish ajax request to asp.net mvc controller , but it give me internal server error

// My Products Controller
[HttpPost]
    public ActionResult FilterCategeory(int prodID) 
    {
        var categs = new Categ() {PROD_ID=prodID }.Search();
        return Json(categs);
    }

//My ajax request 
$("#categs").empty();
    var prm = $("#prods").val();
    $.ajax({
        type: "POST",
        url: '@Url.Action("FilterCategeory", "Products")',
        contentType: "application/json; charset=utf-8",
        data: {prodID: prm },
        dataType: "json",
        success: function (data)
        {
           alert('Success');
        },
        error: function () { alert('error');}
        });

回答1:

The ajax request throws Invalid JSON primitive exception. So Pass the data using JSON.stringify(obj)

Ajax Request

    var prm = $("#prods").val();
    var obj = { prodID: prm };
    $.ajax({
        type: "POST",
        url: '@Url.Action("FilterCategeory", "Home")',
        contentType: "application/json; charset=utf-8",
        data : JSON.stringify(obj),
        dataType: "json",
        success: function (data) {
            alert('Success');
        },
        error: function () { alert('error'); }
    });

Check this question hope it will help you.

You can check the error type in Firefox or Chrome In firefox

Right click the browser click Inspect Element. Then selected the Network tab. When you click the request it will show header, cookies etc. From that choose Response. So you can found the error

In chrome