[英]Posting data using ajax in an MVC controller not working
我在使用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"}
检查您的代码后,我发现了这些-
您没有使用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); } });
您在代码中使用dataType:json
,因此结果将自动解析为json对象。 因此您将无法获得正常的xhr响应,因此xhr.statusText
无效。 结果已经是json
并且由于返回EmptyResult
,因此结果为空,因此data
应为空白或null。
您正在使用EmptyResult
,使用dataType:json
EmptyResult
此选项未连接到服务器,这是为了让jQuery知道您的响应是json
,因此jQuery在调用error
或success
回调时会自动解析它。 所以对于EmptyResult
它是微不足道的
为了使其工作,将EmptyResult
返回EmptyResult
更改为以下内容(这是我使用的语法,还有其他语法,我现在没有安装VS,因此语法可能有点错误,但是您始终可以轻松找到)-
return MVCHelperUtilites.GetJsonResult(new []{ new KeyValuePair<String,String>("Success",True) });
如果您仍然愿意使用EmptyResult
使用dataType:html
代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.