[英]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.