【问题标题】:Posting data using ajax in an MVC controller not working在 MVC 控制器中使用 ajax 发布数据不起作用
【发布时间】:2014-05-05 01:57:15
【问题描述】:

我在使用 ajax 将数据发布到控制器时遇到问题,我试图通过 ajax 将字符串发送到控制器但它甚至没有到达控制器,我的代码是这样的:

var m_page_nm = $('#pagemenu_hid').val(),
oColumns = JSON.stringify(this.oKgrid.columns),
data = JSON.stringify({ columns: oColumns, page_name: m_page_nm, grid_nm:     this.m_kgrid_id });

$.ajax({
    url: "/Favorite/SaveColumnSettings",
    type: 'POST',             
    data:{ gridset:data },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
    error: function (xhr) {
        alert('Error: ' + xhr.statusText);
    },               
});

在控制器中就这么简单

[HttpPost]

public ActionResult SaveColumnSettings(string gridset)
{
    return new EmptyResult();
}

在开发者工具上是这个请求

Request URL:http://localhost:2144/Favorite/SaveColumnSettings
Request Headers CAUTION: Provisional headers are shown.
Accept:*/*
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost:2144
Referer:http://localhost:2144/Agent/Index?menu=AGENT_SETUP
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)             Chrome/34.0.1847.131 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
gridset:{"columns":" [{\"encoded\":true,\"title\":\"Id\",\"hidden\":true,\"field\":\"agent_no\",\"filterable\":{},\"attributes\":{\"style\":\"display:none\"},\"footerAttributes\":{\"style\":\"display:none\"},\"headerAttributes\":{\"style\":\"display:none\"}},{\"encoded\":true,\"title\":\"Agent Code\",\"width\":\"20px\",\"field\":\"agent_cd\",\"groupable\":false,\"filterable\":{}},{\"encoded\":true,\"title\":\"Agent Name\",\"width\":\"80px\",\"field\":\"agent_nm\",\"groupable\":false,\"filterable\":{}},{\"encoded\":true,\"title\":\"Supervisory Code\",\"width\":\"20px\",\"field\":\"supervisor_cd\",\"filterable\":{}},{\"encoded\":true,\"title\":\"Sales Dept. Code\",\"width\":\"20px\",\"field\":\"sdept_cd\",\"filterable\":{}},{\"encoded\":true,\"title\":\"Area\",\"width\":\"20px\",\"field\":\"area_cd\",\"filterable\":{}},{\"encoded\":true,\"title\":\"Active?\",\"width\":\"10px\",\"template\":\"<div style='text-align:center'><input type='checkbox' disabled checked #= inactive_yn? checked='checked': checked='' # class='chkbx' /></div>\",\"field\":\"inactive_yn\",\"filterable\":{}}]","page_name":"AGENT_SETUP","grid_nm":"#agent_kgrid"}

【问题讨论】:

  • 有什么错误吗?或者使用google chrome developer tools(f12)或者firefox firebugs来检查url是否正确
  • 我在开发者工具上没有任何错误。我的问题是,即使 url 正确,它也无法到达我的控制器,当它到达控制器时,参数为空......我已经储存了几天
  • @Se0ng11 我有上面的编辑...我已经发送了那个请求,但它没有到达我的控制器
  • 我看到你的json无效,尝试使用jsoneditoronline.org/index.html之类的工具来检查你的json是否正确,我只能提供帮助
  • @Se0ng11 该网站说我有 3 个对象,这意味着我的 json 是正确的..???列是一个字符串,然后是 page_nm 和 grid_nm

标签: c# jquery ajax asp.net-ajax


【解决方案1】:

检查您的代码后,我发现了这些 -

  1. 你没有使用ajax成功回调方法,所以即使你得到正确的结果你也无法知道。添加success 方法,一个示例就像(使用JS控制台查看结果,可能是FireBug或Chrome开发者工具) -

    $.ajax({ url: "/Favorite/SaveColumnSettings", type: 'POST',
    data:{ gridset:data }, dataType: 'json', contentType: 'application/json; charset=utf-8', error: function (data) { console.log(data); },
    success: function (data) { console.log(data); } });

  2. 你在代码中使用了dataType:json,所以结果会自动解析为json对象。因此您不会得到正常的 xhr 响应,因此 xhr.statusText 无效。结果已经是json,由于您返回了EmptyResult,结果为空,因此data 应该为空或null。

  3. 你正在使用EmptyResult,使用dataType:json - 这个选项没有连接到服务器,这是为了让jQuery知道你的响应是json,所以jQuery会在调用@987654332时自动解析它@ 或 success 回调。所以EmptyResult 微不足道

  4. 为了使它起作用,将 return EmptyResult 更改为类似这样的内容(这是我使用的语法,还有其他的,我现在没有安装 VS,所以语法可能有点错误,但是你总能轻松找到。)-

    return MVCHelperUtilites.GetJsonResult(new []{ new KeyValuePair<String,String>("Success",True) });

  5. 如果您仍愿意使用EmptyResult,请改用dataType:html

【讨论】:

  • 要试试这个@Mahmud
  • 使用 firebug 或 chrome 开发工具查看 ajax 调用的响应。
  • 这是服务器端错误。用VS调试,顺便去掉contentType: 'application/json; charset=utf-8',也没多大用,控制器会自己检测json,我从没用过
  • 我已经调试了 2 天这个问题.. 它没有到达控制器,或者当它到达控制器时它有一个空值
  • 500 表示.. 服务器错误,如果到达它将为空,因为您返回的是空...
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 2014-01-22
相关资源
最近更新 更多