Javascript 错误时从AJAX调用重定向到视图
问题: 我希望在成功时将partialview加载到DIV中,但如果发生错误,我需要重定向到视图主页。问题是重定向到操作也加载到divJavascript 错误时从AJAX调用重定向到视图,javascript,jquery,ajax,asp.net-mvc,redirect,Javascript,Jquery,Ajax,Asp.net Mvc,Redirect,问题: 我希望在成功时将partialview加载到DIV中,但如果发生错误,我需要重定向到视图主页。问题是重定向到操作也加载到divresultdisplay。 我应该如何管理错误处理以在发生错误时重定向到FullView 代码: Ajax调用: 行动: 不用在controller中重定向,您只需将信息发送回客户端ajax请求,并按如下方式处理重定向: function GetFilteredValuesCallback(values) { var data = JSON.string
resultdisplay
。
我应该如何管理错误处理以在发生错误时重定向到FullView
代码:
Ajax调用:
行动:
不用在controller中重定向,您只需将信息发送回客户端ajax请求,并按如下方式处理重定向:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
if(result.message==="Failed")
location.href = "yoururl"
else
$('#resultDisplay').html(result);
},
error:function(result)
{
//handle any errors
}
});
}
控制器
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return Json(new{message="Failed"}, JsonRequestBehavior.AllowGet); //Send a message back
}
return PartialView("_PartialView", model);
}
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return new HttpStatusCodeResult(500);
}
return PartialView("_PartialView", model);
}
更新
感谢@StephenMuecke建议以更优雅的方式处理此问题,具体操作如下:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
if(result.message==="Failed")
location.href = "yoururl"
else
$('#resultDisplay').html(result);
},
error:function(result)
{
//handle any errors
}
});
}
JS
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
$('#resultDisplay').html(result);
},
error:function(result)
{
location.href("yoururl");
}
});
}
控制器
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return Json(new{message="Failed"}, JsonRequestBehavior.AllowGet); //Send a message back
}
return PartialView("_PartialView", model);
}
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return new HttpStatusCodeResult(500);
}
return PartialView("_PartialView", model);
}
不用在controller中重定向,您只需将信息发送回客户端ajax请求,并按如下方式处理重定向:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
if(result.message==="Failed")
location.href = "yoururl"
else
$('#resultDisplay').html(result);
},
error:function(result)
{
//handle any errors
}
});
}
控制器
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return Json(new{message="Failed"}, JsonRequestBehavior.AllowGet); //Send a message back
}
return PartialView("_PartialView", model);
}
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return new HttpStatusCodeResult(500);
}
return PartialView("_PartialView", model);
}
更新
感谢@StephenMuecke建议以更优雅的方式处理此问题,具体操作如下:
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
if(result.message==="Failed")
location.href = "yoururl"
else
$('#resultDisplay').html(result);
},
error:function(result)
{
//handle any errors
}
});
}
JS
function GetFilteredValuesCallback(values) {
var data = JSON.stringify(values);
var url = '/Controller/Action';
$.ajax({
type: 'GET',
url: url,
data: { filter: data },
success: function (result) {
$('#resultDisplay').html(result);
},
error:function(result)
{
location.href("yoururl");
}
});
}
控制器
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return Json(new{message="Failed"}, JsonRequestBehavior.AllowGet); //Send a message back
}
return PartialView("_PartialView", model);
}
public ActionResult Action(string filter)
{
MyModel model = null;
try
{
// Do stuff with my model ...
throw new Exception("ERROR!");
}
catch (Exception ex)
{
// In case of error
return new HttpStatusCodeResult(500);
}
return PartialView("_PartialView", model);
}
服务器端
RedirectToAction
向浏览器返回重定向响应,然后Ajax会立即忽略该响应。您需要将元数据返回到Ajax调用,以便它知道如何将页面更改为新的URL(或者使用错误并在错误:
回调中重定向)。Ajax调用保持在同一页面上。不要捕获异常,而是让它发生,然后在ajax.fail()
回调中,使用location.href='yourUrl'
Server-sideRedirectToAction
向浏览器返回重定向响应,然后Ajax会立即忽略该响应。您需要将元数据返回到Ajax调用,以便它知道如何将页面更改为新的URL(或者使用错误并在错误:
回调中重定向)。Ajax调用保持在同一页面上。不要捕获异常,而是让它发生,然后在ajax.fail()
回调中,使用location.href='yourUrl'代码>