gpt4 book ai didi

javascript - 为什么 'response' 在当前上下文中不可用?

转载 作者:行者123 更新时间:2023-12-01 01:10:58 25 4
gpt4 key购买 nike

我这里有以下代码片段。我正在尝试使用 /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.

我哪里出错了吗?

最佳答案

response 是一个包含 AJAX 响应的客户端变量,因此您不能将其用作 @Url.Action()< 内的 routeValues 参数值 帮助程序,其中包含服务器端代码,因为在生成操作 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");
}
}
});

或者使用服务器端的占位符,然后使用 replace() 将参数值更改为 response:

$.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");
}
}
});

关于javascript - 为什么 'response' 在当前上下文中不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55150949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com