我在这里有以下代码段。当/Events/Index
请求成功时,我试图用Ajax
页面刷新页面。但是,在success
方法内部,我看到response
变量在else if
情况下可用,但在if
情况下不可用。在if
情况下,出现错误:The name response does not exist in the current context
。
从Ajax
进行的View
调用如下:
$.ajax({ url: "/Events/DeleteEvent", data:data, async: true }).success(function (response) {
if (response != "" || response != "Event not found!") {
swal("Deleted!", "The event has been deleted.", "success");
window.location.href = '@Url.Action("Index", "Events", new { EventId = response })';
}
else if (response == "Event not found")
swal("Cancelled!!!", "Error : " + response, "error");
});
这是我从success
向Ajax
调用的Controller
部分发送回复的方式:
if (eventid > 0)
{
...
return Json(id);
}
else
return Json("Event not found");
// id is an integer value that I want to send to success in Ajax.
我在哪里出错了?
答案 0 :(得分:1)
尝试一下:
$.ajax({
url: "/Events/DeleteEvent",
data: data,
async: true,
success: function (response) {
if (response !== "" || response != "Event not found!") {
swal("Deleted!", "The event has been deleted.", "success");
window.location.href = '@Url.Action("Index", "Events", new { EventId = "' + response + '" })';
} else if (response == "Event not found") {
swal("Cancelled!!!", "Error : " + response, "error");
}
}
});
您的语法有误。查看代码,您会发现语法上的区别。
让我知道这是怎么回事。
答案 1 :(得分:1)
response
是包含AJAX响应的客户端变量,因此您不能将其用作包含服务器端代码的routeValues
帮助器内的@Url.Action()
参数值,因为脚本没有生成操作URL时尚未执行,并且服务器端代码中尚未声明response
变量。
要解决此问题,请尝试使用普通查询字符串插入EventId
参数:
$.ajax({
url: "/Events/DeleteEvent",
data: data,
async: true,
success: function (response) {
if (response !== "" || response != "Event not found!") {
swal("Deleted!", "The event has been deleted.", "success");
// use query string because Url.Action helper runs server-side
window.location.href = '@Url.Action("Index", "Events")' + '?EventId=' + response;
} else if (response == "Event not found") {
swal("Cancelled!!!", "Error : " + response, "error");
}
}
});
或者在服务器端使用占位符,然后使用response
将参数值更改为replace()
:
$.ajax({
url: "/Events/DeleteEvent",
data: data,
async: true,
success: function (response) {
if (response !== "" || response != "Event not found!") {
swal("Deleted!", "The event has been deleted.", "success");
// the URL generated server-side with placeholder
var targetUrl = '@Url.Action("Index", "Events", new { EventId = "xxx" })';
// replace placeholder with event ID
window.location.href = targetUrl.replace("xxx", response);
} else if (response == "Event not found") {
swal("Cancelled!!!", "Error : " + response, "error");
}
}
});
附加说明:
更好地在响应中使用客户端属性来区分成功和错误情况,如下例所示:
if (eventid > 0)
{
...
return Json(new { id = id });
}
else
return Json(new { message = "Event not found" });
AJAX通话
$.ajax({
url: '@Url.Action("DeleteEvent", "Events")',
data: data,
async: true,
success: function (response) {
if (typeof response.id !== 'undefined' && response.id != null) {
swal("Deleted!", "The event has been deleted.", "success");
// use query string because Url.Action helper runs server-side
window.location.href = '@Url.Action("Index", "Events")' + '?EventId=' + response.id;
} else if (typeof response.message !== 'undefined' && response.message != null) {
swal("Cancelled!!!", "Error : " + response.message, "error");
}
}
});
答案 2 :(得分:0)
传递响应值而不是其名称
'@Url.Action("Index", "Events", new { EventId = "' + response + '" })'