为什么在 Javascript 函数中通过 Ajax 调用时我的 Razor 控制器操作调用不起作用?
Why is my Razor controller action call not working when called via Ajax in a Javascript function?
我有一个 Asp.NetCore Razor 应用程序。
我试图通过 $ajax() 调用我的控制器来调用控制器操作。
该操作未进入我的控制器。
我觉得 javascript 函数中的 @Url.Action() 配置方式有问题。
它应该是 @Url.Action(ActionName, Controller).
的形式
我在 devtools window 控制台中没有看到任何错误。
我在这里做错了什么?
这是包含我在控制器上的 DeleteCompany 操作的 C# 代码片段:
public class CompanyController : BaseController
{
[HttpPost]
public async Task<IActionResult> DeleteCompany(Guid companyId)
{
// Do some stuff
return RedirectToAction("Index");
}
}
这是我的 html 片段,其中调用了 ConfirmDelete() 函数:
<text>
<input type="hidden" value="<%- data.CompanyId %>" name="CompanyId" />
<input type="image" value="<%- data.CompanyId %>" src="/icon/close.png" width="25" height="25"
onclick="ConfirmDelete('<%- data.CompanyId %>', '<%- data.CompanyName %>')"/>
</text>
这是我的 javascript 提示确认并 ajax 调用控制器:
<script>
function ConfirmDelete(companyId, companyName) {
var message = "<i>Are you sure you want to delete Customer: ".concat(companyName).concat("?");
var result = DevExpress.ui.dialog.confirm(message, "Confirm Company Delete");
result.done(function (dialogResult) {
$.ajax({
url: '@Url.Action("DeleteCompany","Company")',
data: { companyId: companyId },
success: function(data) {
//call is successfully completed and we got result in data
},
error:function (xhr, ajaxOptions, thrownError) {
//some errror, some show err msg to user and log the error
alert(xhr.responseText);
}
});
});
};
</script>
首先你应该在你的ajax中添加type:"Post"
,然后ajax不支持重定向,你应该在Ajax的成功函数中重定向。
您可以像下面这样更改您的代码。
$.ajax({
url: '@Url.Action("DeleteCompany","Company")',
data: { companyId: companyId },
type:"Post",
success: function() {
location.href = '@Url.Action("Index","Company")'
},
error:function (xhr, ajaxOptions, thrownError) {
//some errror, some show err msg to user and log the error
alert(xhr.responseText);
}
});
那么在你的行动中:
[HttpPost]
public async Task<IActionResult> DeleteCompany(Guid companyId)
{
// Do some stuff
return Ok();
}
我有一个 Asp.NetCore Razor 应用程序。 我试图通过 $ajax() 调用我的控制器来调用控制器操作。 该操作未进入我的控制器。 我觉得 javascript 函数中的 @Url.Action() 配置方式有问题。 它应该是 @Url.Action(ActionName, Controller).
的形式我在 devtools window 控制台中没有看到任何错误。 我在这里做错了什么?
这是包含我在控制器上的 DeleteCompany 操作的 C# 代码片段:
public class CompanyController : BaseController
{
[HttpPost]
public async Task<IActionResult> DeleteCompany(Guid companyId)
{
// Do some stuff
return RedirectToAction("Index");
}
}
这是我的 html 片段,其中调用了 ConfirmDelete() 函数:
<text>
<input type="hidden" value="<%- data.CompanyId %>" name="CompanyId" />
<input type="image" value="<%- data.CompanyId %>" src="/icon/close.png" width="25" height="25"
onclick="ConfirmDelete('<%- data.CompanyId %>', '<%- data.CompanyName %>')"/>
</text>
这是我的 javascript 提示确认并 ajax 调用控制器:
<script>
function ConfirmDelete(companyId, companyName) {
var message = "<i>Are you sure you want to delete Customer: ".concat(companyName).concat("?");
var result = DevExpress.ui.dialog.confirm(message, "Confirm Company Delete");
result.done(function (dialogResult) {
$.ajax({
url: '@Url.Action("DeleteCompany","Company")',
data: { companyId: companyId },
success: function(data) {
//call is successfully completed and we got result in data
},
error:function (xhr, ajaxOptions, thrownError) {
//some errror, some show err msg to user and log the error
alert(xhr.responseText);
}
});
});
};
</script>
首先你应该在你的ajax中添加type:"Post"
,然后ajax不支持重定向,你应该在Ajax的成功函数中重定向。
您可以像下面这样更改您的代码。
$.ajax({
url: '@Url.Action("DeleteCompany","Company")',
data: { companyId: companyId },
type:"Post",
success: function() {
location.href = '@Url.Action("Index","Company")'
},
error:function (xhr, ajaxOptions, thrownError) {
//some errror, some show err msg to user and log the error
alert(xhr.responseText);
}
});
那么在你的行动中:
[HttpPost]
public async Task<IActionResult> DeleteCompany(Guid companyId)
{
// Do some stuff
return Ok();
}