如何在Ajax场景中返回一个错误 [英] How to return an error in an Ajax scenario

查看:105
本文介绍了如何在Ajax场景中返回一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC使用jQuery。我有以下的MVC的行动是成功返回部分页面。在应用程序错误,我不知道该怎么给它发在客户端正确处理它:

 公众的ActionResult LoadFilterSet(INT filterSetId)
{
    尝试
    {
        BreadCrumbManager bcManager = this.ResetBreadCrumbManager(this.BreadCrumbManagerID);
        GeneralHelper.LoadBreadCrumbManager(bcManager,filterSetId);

        计算机[BreadCrumbManager] = bcManager;
        返回视图(LoadFilterSet);
    }
    赶上(例外前)
    {
        返回内容();
    }
}
 

以下是我的jQuery ajax调用。请注意,我检查的数据长度,以确保没有错误。请建议我这样做的更好的方法。

  $。阿贾克斯({
    键入:GET,
    数据类型:HTML,
    异步:真正的,
    数据:({filterSetId:selectedId}),
    网址:链接,
    的contentType:text / html的;字符集= UTF-8,
    成功:功能(数据,textStatus){
        如果(data.length大于0){
            //清除第一个局部筛选器。
            clearLocalFilters();
            $('td.selected过滤器table.filters显示)追加(数据);
        }
    }
});
 

解决方案

我会在你的Ajax调用的设置添加一个错误的功能。让服务器来决定错误信息显示和传递了ajax错误处理程序,让它显示出来。

 成功:功能(数据,textStatus){
    //清除第一个局部筛选器。
    clearLocalFilters();
    $('td.selected过滤器table.filters显示)追加(数据);
},
错误:功能(数据){
    警报(data.responseText); //这里使用任何显示逻辑
}
 

在你的控制器的动作,如果发现错误

  Response.Status code =(INT)的HTTPStatus code.BadRequest;
返回内容(的errorMessage,MediaTypeNames.Text.Plain);
 

I am using ASP.NET MVC with jQuery. I have the following MVC Action that returns a partial page on Success. On Application Error, I am not sure what to send it for correctly handling it at the client side:

public ActionResult LoadFilterSet(int filterSetId)
{
    try
    {
        BreadCrumbManager bcManager = this.ResetBreadCrumbManager(this.BreadCrumbManagerID);
        GeneralHelper.LoadBreadCrumbManager(bcManager, filterSetId);

        ViewData["BreadCrumbManager"] = bcManager;
        return View("LoadFilterSet");
    }
    catch (Exception ex)
    {
        return Content("");
    }
}

Following is my jQuery ajax call. Notice that I am checking for the data length to make sure there are no errors. Please suggest me a better way of doing this.

$.ajax({
    type: "GET",
    dataType: "html",
    async: true,
    data: ({ filterSetId: selectedId }),
    url: link,
    contentType: "text/html; charset=utf-8",
    success: function(data, textStatus) {
        if (data.length > 0) {
            // Clear the local filters first.
            clearLocalFilters();
            $('td.selected-filters table.filters-display').append(data);
        }
    }
});

解决方案

I would add an error function in your setup of the ajax call. Let the server determine the error message to display and pass it the ajax error handler and let it display it.

success: function(data, textStatus) {     
    // Clear the local filters first.     
    clearLocalFilters();     
    $('td.selected-filters table.filters-display').append(data);         
},
error: function (data) { 
    alert(data.responseText); // use any display logic here
}

In your controller's action, if an error is found

Response.StatusCode = (int)HttpStatusCode.BadRequest; 
return Content(errorMessage, MediaTypeNames.Text.Plain);

这篇关于如何在Ajax场景中返回一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆