2

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');}
        });
Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
Ziad Elnaggar
  • 73
  • 1
  • 1
  • 6
  • 1
    Internal server error means your throwing an exception in the controller - debug your code! And whats the point of `string prodID` then `prodID.ToInt32()` - if its an `int` then make the parameter `int prodID`. And there is no point in `JsonRequestBehavior.AllowGet` if its a POST –  May 03 '15 at 07:21
  • my controller returns list of products correctly , but i think the problem related to (return Json(categs);) it didn't return json object but it returns c# JsonResult Object , thus i think that jquery couldn't resolve the returned object – Ziad Elnaggar May 03 '15 at 07:38
  • Its supposed to return `object` [Refer the docs](https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.118).aspx) –  May 03 '15 at 07:48
  • What is `categs`? (show your models) –  May 03 '15 at 08:00
  • Normally, HTTP 500 internal server error happens if there is any problem in you configuration file. – Adersh M May 03 '15 at 09:39
  • @StephenMuecke Categ refers to Category , it's a class which contains Category fields like category_id,category_name ...etc – Ziad Elnaggar May 03 '15 at 15:47
  • @AdershM i have no problems in my configuration files :( – Ziad Elnaggar May 03 '15 at 15:48
  • @ZiadElnaggar,does a category happens to contains other categories (e.g. a property which is a collection of child categories) in which case you will have a circular reference exception? –  May 03 '15 at 21:55

1 Answers1

7

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

enter image description here

In chrome enter image description here

Community
  • 1
  • 1
Golda
  • 3,823
  • 10
  • 34
  • 67