weixin_33681778 2015-03-23 06:21 采纳率: 0%
浏览 27

MVC Ajax发布错误

I am getting error while I post Ajax request to controller method. In controller method I need to pass Model class object. But it gives me 500 Internal server error. Can anybody help me to make it correct?

Mu code is as per below:

jQuery:

 var request = $("#frmHost").serialize();


$.ajax({
        url: "/Host/HostItemDetails/" ,
        type: "POST",
        datatype: 'json',
        contentType : "application/json",
        data: request,
        success: function (data) {
            if (data == '1111') {
                  ///Success code here
            }
            else if (data != '') {
                jAlert(data);
            }
        }
    });

Controller Method :
[HttpPost]
public JsonResult HostItemDetails(ClsHost objHost)
{
     //Code here
 return Json("1111");
}
  • 写回答

3条回答 默认 最新

  • weixin_33709609 2015-03-23 08:08
    关注

    I ran the same code that you are running. To test the code I did the following changes. I took a button, and on click event I am sending the post back to the controller. the '[HttpPost]' attribute is fine too.

    Can you make one thing sure, that the frmHost data matches to the class ClsHost,but still that shouldn't cause the server error, the error will be different.

    enter image description here

    $(document).ready(function () {
        $("#clickMe").click(function () {
            var request = '{"Users":[{"Name":"user999","Value":"test"},{"Name":"test2","Value":"test"}]}';
            $.ajax({
                url: "/Home/HostItemDetails/",
                type: "POST",
                datatype: 'json',
                contentType: "application/json",
                data: request,
                success: function (data) {
                    if (data == '1111') {
                        ///Success code here
                    }
                    else if (data != '') {
                        jAlert(data);
                    }
                }
            });
        });
    
    });
    

    Controller:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonResult HostItemDetails(ClsHost objHost)
    {
        //Code here
        return Json("111", JsonRequestBehavior.AllowGet);   
    }
    
    评论

报告相同问题?