[英]HttpPost in MVC Controller not being called through Ajax POST
[英]MVC controller method is not being called from ajax
MVC控制器方法未從我已聲明的Ajax中調用。 PFB代碼片段C#控制器:
public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId)
{
MyDetails obj = new MyDetails();
List<string> Lst = new List<string>();
Lst = obj.Check(AssociateId, AssetId, ReqID);
return this.Json(Lst, "text/json");
}
Javascript代碼(ajax調用):引用詳細信息控制器和webmethod Checkfunction
$.ajax({
type: 'GET',
cache: false,
url: '@Url.Action("Details/Checkfunction")',
data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name },
contentType: "application/json",
success: function (data) {
debugger;
if (data.length > 0) {
ViewModel.REQUESTID() = data[0];
ViewModel.FLAG() = '1';
}
else {
debugger;
ViewModel.FLAG() = '0';
ViewModel.REQUESTID() = '';
}
if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) {
debugger;
ViewModel.REQID() = RequestId;
}
},
error: function (error) {
alert("error");
}
});
嘗試這個:
$.ajax({
type: 'POST',
cache: false,
url: '/PhoenixInbox/Checkfunction',
data: { 'ReqID': RequestId, 'AssociateId': AssociateID, 'AssetId': Host_Name },
contentType: "application/json",
success: function (data) {
debugger;
if (data.length > 0) {
ViewModel.REQUESTID() = data[0];
ViewModel.FLAG() = '1';
}
else {
debugger;
ViewModel.FLAG() = '0';
ViewModel.REQUESTID() = '';
}
if (ViewModel.REQUESTID() != '' || ViewModel.REQUESTID() != null) {
debugger;
ViewModel.REQID() = RequestId;
}
},
error: function (error) {
alert(JSON.stringify(error));
}
});
控制器:
[Httppost]
public ActionResult Checkfunction(string ReqID, string AssociateId, string AssetId)
{
MyDetails obj = new MyDetails();
List<string> Lst = new List<string>();
Lst = objMyAssetsDetails.Check(AssociateId, AssetId, ReqID);
return this.Json(Lst, "text/json");
}
如今最好使用Promise,如果要返回json,最好返回JsonResult而不是ActionResult
正確建立網址:
$ .ajax({type:'POST',cache:false,url:' @ Url.AbsoluteAction(“ PhoenixInbox”,“ Checkfunction”) ',
確保您允許獲取Get操作: JsonRequestBehavior.AllowGet
公共ActionResult Checkfunction(字符串ReqID,字符串AssociateId,字符串AssetId){MyDetails obj = new MyDetails(); List Lst =新的List(); Lst = objMyAssetsDetails.Check(AssociateId,AssetId,ReqID); 返回this.Json(Lst,“ text / json”,JsonRequestBehavior.AllowGet); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.