我正在使用AJAX調用來顯示基於用C#編寫的某些邏輯的成功或失敗的信息。我現在需要從服務器返回附加數據並顯示它。我需要的數據包含在employers
和agencies
變量中。如何將return Json
聲明中的數據與success
一起退回?在C#中通過AJAX從服務器返回數據
$.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);
將'employers'和'agencies'變量添加到您提供給Json響應的匿名類型。然後可以訪問它們在JavaScript的'data'變量中公開的屬性/對象/數組。你不可能給你更具體的東西,因爲你沒有向我們展示這些類實際上是什麼樣的。 –
看看你用C#代碼中的'error'成員做了什麼?對其他人也做同樣的事情。 –
那會怎樣?像這樣? return Json(new {success = true,employers = employer,agencies = agencies},JsonRequestBehavior.AllowGet); – noclist