gpt4 book ai didi

c# - 通过 AJAX 从 C# 中的服务器返回数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:48 25 4
gpt4 key购买 nike

我正在使用 AJAX 调用来显示基于某些用 C# 编写的逻辑的成功或失败的信息。我现在需要从服务器返回额外的数据并显示它。我需要的数据包含在 employersagencies 变量中。如何在 return Json 语句中返回该数据以及 success

$.ajax({
url: rootpath + "/clients/hasDuplicateTaxId",
type: "GET",
success: function (data) {
if (data.success) {
// success
}
else {
// fail
}
},
error: function (response) {
var error = response;
}
});
if (taxIdExists)
{
var employers = _employerRepository.GetByTaxId(taxId).ToList();
var agencies = _employerRepository.GetByTaxId(taxId).Select(e => e.GeneralAgency).ToArray();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}

return Json(new { success = false, error = "Error" }, JsonRequestBehavior.AllowGet);

最佳答案

您需要将 employersagencies 变量添加到您提供给 Json 的匿名类型中,如下所示:

if (taxIdExists)
{
var employers = _employerRepository.GetByTaxId(taxId).ToList();
var agencies = _employerRepository.GetByTaxId(taxId).Select(e => e.GeneralAgency).ToArray();
return Json(new { success = true, employers, agencies }, JsonRequestBehavior.AllowGet);
}

从那里您可以访问存储在 $.ajax 调用的 success 处理程序中的那些属性中的数组:

success: function (data) {
if (data.success) {
console.log(data.employers);
console.log(data.agencies);
}
else {
// fail
}
},

它们将是数组,因此您需要遍历它们以提取所需信息。

关于c# - 通过 AJAX 从 C# 中的服务器返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34951745/

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