jQuery 在 ASP.Net MVC 5 中获取请求
jQuery get request in ASP.Net MVC 5
我正在使用 ASP.Net MVC 5。我有一个 table,其中包含角色 Table 值。
当我单击编辑按钮时,它会发送 jQuery 请求以获取数据,它会 return 像 "UMS.Entities.Role"
这样的数据,但我没有从中获取值,但是我从数据库中得到了礼仪响应,但我jQuery.
遇到问题
function dataForEdit(id) {
var data = GetRequestedData('/Admin/Role/Edit/' + id);
$("#create-form").slideDown();
$('.formEditBtn').slideDown();
setInputValue('#role-name', data.Name);
$("#roleID").attr("value", data.RoleId);
}
GetRequestedData('/Admin/Role/Edit/' + id)
是单独的函数,它是单独的文件。
function GetRequestedData(url) {
$.ajax({
url: url,
async: false,
type: 'GET',
success: function (data) {
resultData = data; // here I am getting this UMS.Entities.Role
},
error: function (error) {
$(this).remove();
alert(error.statusText);
}
});
return resultData
}
我的控制器功能是
public Role Edit(int id) {
Role role = _roleService.GetById(id);
return role;
}
我认为你应该 return Json 结果,你可以试试这个吗?
public ActionResult Edit(int id)
{
Role role = _roleService.GetById(id);
return Json(role , JsonRequestBehavior.AllowGet);
}
首先,您使用的是 async: false
,这是非常糟糕的做法。正确使用异步模式并在 AJAX 调用完成后使用回调来执行逻辑。
也就是说,您代码中的主要问题是因为 Role
class 正在通过 ToString()
序列化。将它序列化为 JSON 更适用,像这样:
public ActionResult Edit(int id)
{
Role role = _roleService.GetById(id);
return Json(role, JsonRequestBehavior.AllowGet);
}
然后在您的 JS 代码中,您可以从提供给 success
回调的 data
参数中的 Role
class 检索属性:
function dataForEdit(id) {
GetRequestedData('/Admin/Role/Edit/' + id, function(data) {
$("#create-form, .formEditBtn').slideDown();
setInputValue('#role-name', data.Name);
$("#roleID").val(data.RoleId);
});
}
function GetRequestedData(url, callback) {
$.ajax({
url: url,
type: 'GET',
success: callback,
error: function(error) {
$(this).remove();
alert(error.statusText);
}
});
}
GetRequestedData('/Admin/Role/Edit/' + id);
我正在使用 ASP.Net MVC 5。我有一个 table,其中包含角色 Table 值。
当我单击编辑按钮时,它会发送 jQuery 请求以获取数据,它会 return 像 "UMS.Entities.Role"
这样的数据,但我没有从中获取值,但是我从数据库中得到了礼仪响应,但我jQuery.
function dataForEdit(id) {
var data = GetRequestedData('/Admin/Role/Edit/' + id);
$("#create-form").slideDown();
$('.formEditBtn').slideDown();
setInputValue('#role-name', data.Name);
$("#roleID").attr("value", data.RoleId);
}
GetRequestedData('/Admin/Role/Edit/' + id)
是单独的函数,它是单独的文件。
function GetRequestedData(url) {
$.ajax({
url: url,
async: false,
type: 'GET',
success: function (data) {
resultData = data; // here I am getting this UMS.Entities.Role
},
error: function (error) {
$(this).remove();
alert(error.statusText);
}
});
return resultData
}
我的控制器功能是
public Role Edit(int id) {
Role role = _roleService.GetById(id);
return role;
}
我认为你应该 return Json 结果,你可以试试这个吗?
public ActionResult Edit(int id)
{
Role role = _roleService.GetById(id);
return Json(role , JsonRequestBehavior.AllowGet);
}
首先,您使用的是 async: false
,这是非常糟糕的做法。正确使用异步模式并在 AJAX 调用完成后使用回调来执行逻辑。
也就是说,您代码中的主要问题是因为 Role
class 正在通过 ToString()
序列化。将它序列化为 JSON 更适用,像这样:
public ActionResult Edit(int id)
{
Role role = _roleService.GetById(id);
return Json(role, JsonRequestBehavior.AllowGet);
}
然后在您的 JS 代码中,您可以从提供给 success
回调的 data
参数中的 Role
class 检索属性:
function dataForEdit(id) {
GetRequestedData('/Admin/Role/Edit/' + id, function(data) {
$("#create-form, .formEditBtn').slideDown();
setInputValue('#role-name', data.Name);
$("#roleID").val(data.RoleId);
});
}
function GetRequestedData(url, callback) {
$.ajax({
url: url,
type: 'GET',
success: callback,
error: function(error) {
$(this).remove();
alert(error.statusText);
}
});
}
GetRequestedData('/Admin/Role/Edit/' + id);