Asp.net mvc 4 jquery ajax request return internal server error

对着背影说爱祢 提交于 2019-12-13 17:01:52

问题


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



来源:https://stackoverflow.com/questions/30011229/asp-net-mvc-4-jquery-ajax-request-return-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!