在我的ASP.NET MVC应用程序中,我在View的脚本部分中有这个AJAX调用:
$(".ckbx").change(function () {
. . .
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: { unit: unitval, report: rptval },
cache: false,
success: function (result) {
alert(result);
}
});
});
逐步调用Controller动作:
public ActionResult GetUnitReportPairVals(string unit, string report)
{
HomeModel model = new HomeModel();
int rptId = GetReportIDForName(report);
DataTable UnitReportPairEmailValsDT = new DataTable();
UnitReportPairEmailValsDT = SQL.ExecuteSQLReturnDataTable(
SQL.UnitReportPairEmailQuery,
CommandType.Text,
new SqlParameter()
{
ParameterName = "@Unit",
SqlDbType = SqlDbType.VarChar,
Value = unit
},
new SqlParameter()
{
ParameterName = "@RptID",
SqlDbType = SqlDbType.Int,
Value = rptId
}
);
model.UnitReportPairEmailVals = UnitReportPairEmailValsDT;
return View(model);
}
......一切似乎都很好; “model”包含UnitReportPairEmailVals中的预期值。
但我从来没有在Ajax调用中看到来自此处的警报消息:
success: function (result) {
alert(result);
}
那么为什么没有达到jQuery AJAX调用的“成功”功能呢?
return View(model);
...将“查看”这个词描绘成罗德岛公鸡的梳子。但如果不应该那样,它应该是什么?这与我的“公共ActionResult Index()”控制器Action的结尾完全相同,它运行良好。
我将Ajax调用的结尾更改为:
success: function (result) {
alert(result);
},
failure: function (error) {
alert(error);
}
...我的Controller方法结束:
return Json(new { Result = model }, JsonRequestBehavior.AllowGet);
......但我既没有成功,也没有成功,也没有失败。
注意:当我尝试这个时:
var model = JSON.stringify({ unit: unitval, report: rptval });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: model,
contentType: 'application/json',
cache: false,
success: function (result) {
alert(result);
},
error: function (result) {
debugger;
}
});
...“unit”为空,所以根本不会飞。
当我将其更改为此时(删除第1行并将“数据”arg更改回我以前使用过的内容):
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: { unit: unitval, report: rptval },
contentType: 'application/json',
cache: false,
success: function (result) {
alert(result);
},
error: function (result) {
alert('failed');
}
});
...“单位”是我所期望的(并且“报告”也是如此),但我最终看到'失败'。
在我将这个问题和相关问题的几个不同答案拼凑在一起后,我写了一篇关于如何执行此操作的提示here。
我有[工作] ajax调用[working]:
var model = JSON.stringify({ unit: unitval, report: 1 });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitDataRangeParamsVals", "UnitReportDataRangeParams")',
data: { unit: unitval, report: 1 },
contentType: 'application/json',
cache: false,
success: function (returneddata) {
populatedatarangeprams(1, returneddata);
},
error: function () {
alert('error - returneddata.error.stringify');
}
});
...但我没有使用声明的“模型”,根据我的理解,它与“contentType:'application / json',”
配对所以我认为我应该做的是这样的事情:
var model = JSON.stringify({ unit: unitval, report: 1 });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitDataRangeParamsVals", "UnitReportDataRangeParams")',
data: model,
contentType: 'application/json',
cache: false,
success: function (returneddata) {
populatedatarangeprams(1, returneddata);
},
error: function () {
alert('error - returneddata.error.stringify');
}
});
现在传递给控制器方法的数据采用json字符串格式(封装在“model”中)。
但是,它甚至不起作用。
它适用于第一个块,但不适用于第二个块。
所以我删除了“model”和“contentType”行,并将“data”行改回原来的样子,并且工作正常。那么他们没用,还是我错误地雇用了他们?
答案 0 :(得分:1)
以下是我在ASP.NET MVC中执行ajax时使用的几个片段。
$(".ckbx").change(function () {
.....
var model = JSON.stringify({ unit: unitval, report: rptval });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: model,
contentType: 'application/json',
cache: false,
success: function (result) {
alert(result);
},
error: function(result) {
debugger;
}
});
});
有时这两个更改(与原始版本相比)不是必需的,但如果json不正确,ASP.NET可能会有点挑剔,这对此有所帮助。由于您可以进入控制器方法,这不是您的问题,但它可以很方便。
你真正的问题是控制器方法的返回类型。 ActionResult意味着它将查找相应的视图并根据该视图构建html。但是如果你正在做ajax那么你可能不希望这样。
public JsonResult GetUnitReportPairVals(string unit, string report)
{
// do some stuff
// then create response model
var model = ...//whatever data you are returning here
return Json(model);
}
这是ASP.NET MVC中ajax调用的“标准”方法。
正如您在评论中指出的那样,ActionResult在Index方法中运行良好。是的,因为索引方法的目的是加载页面。通常,如果该方法用于加载页面,那么您将使用ActionResult,因为它将使用视图为该页面创建html。但是,如果您只是提交信息然后返回“成功”之类的响应,那么您不需要整个HTML响应,您需要json。这就是JsonResult在这里工作的原因。
答案 1 :(得分:1)
我怀疑您没有为此操作创建视图,并且在异常情况下会导致ajax调用失败,因为razor引擎无法找到View。确保此操作存在View,或者如果要返回其他视图,请指定:
box_watershed<-read.table("All_EVI_Watersheds_Boxplot.txt", header=TRUE)
ggplot(data = box_watershed, aes(x=box_watershed$ID, y=box_watershed$EVI)) + geom_boxplot(aes(fill=box_watershed$Drought))
p <- ggplot(data = box_watershed, aes(x=box_watershed$ID, y=box_watershed$EVI)) +
geom_boxplot(aes(fill=box_watershed$Drought))
p + facet_wrap( ~ EVI, scales="free")
您还可以添加失败回调以查看出现了什么问题:
return View(model,"ViewName");
如果您只想返回数据,请使用Json:
success: function (result) {
alert(result);
},
error: function(error){
console.log(error);
}