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);   
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退