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);   
    }
    
    评论
  • weixin_33725239 2015-03-23 10:05
    关注

    Nirav Try this,

    Parse the serialized data as a JSON object and later stringify that while posting using JSON.stringify().

    $("#Button").click(function () {
           var data = $("#frmHost").serialize().split("&");
           var request = {};
           for (var key in data) {
                request[data[key].split("=")[0]] = data[key].split("=")[1];
           }
    
           $.ajax({
                url: "/Home/HostItemDetails/",
                type: "POST",
                datatype: 'json',
                contentType: "application/json",
                data: JSON.stringify(request),
                success: function (data) {
                   if (data == '1111') {
                      ///Success code here
                   }
                   else if (data != '') {
                      jAlert(data);
                   }
                }
          });
      });
    
    评论
  • weixin_33695450 2015-06-25 09:21
    关注

    It is solved by removing same property name in Model class. It is mistakenly added by me twice.

    评论

报告相同问题?

悬赏问题

  • ¥20 用C语言解决编程问题
  • ¥15 unity硬件连接与使用
  • ¥15 鱼缸加热棒的数据分析或者实际案例也行
  • ¥15 postgresql11安装完成后,pgadmin无法启动
  • ¥15 (标签-无人机|关键词-Matlab代码)
  • ¥15 执行shell脚本提示参数太多
  • ¥15 困扰好久了,电脑的串口调试助手向单片机发送一个字符(如:字符‘1’到‘8’),单片机点亮对应的灯(如发送字符‘2’则点亮第2个灯)并给电脑回复信息:已点亮第x个灯(其中 x 要替换成对应的数字)
  • ¥15 求减少这个c++程序内存的解决方案
  • ¥30 改正代码,错误比较多
  • ¥15 vue3 keepalive