【问题标题】:After Exception fired it need to send Ajax Error异常触发后需要发送 Ajax 错误
【发布时间】:2017-08-31 23:07:44
【问题描述】:

我有一个 Ajax 方法,当我捕获控制器端异常时。我想将该异常传递给 Ajax 错误方法。 在这里它在异常触发后什么都不做。

Ajax 调用

function ConNews(){
    $.ajax({
        url: $("#getnewsdtl").val(),
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(newsModelLists),
        dataType: "json",
        success: function (news) {
// some code here.
},
 error: function (jqXHR, textStatus, errorThrown) 
{

// I want get controller catched exception here.
}

});

控制器

[HttpPost]
public string GetNews(List<NewsModel> newsModelList)
{
try
{
      return JsonConvert.SerializeObject(newsModelList);
}
catch (Exception ex)
{

  return  JsonConvert.SerializeObject("adf"); // I want to send thisone to Ajax error function
}
}

【问题讨论】:

  • 在你的 catch 块中,你发回一个有效值,所以它永远不会命中 error 块(只有在出现异常时才会被命中)
  • 如何将此值发送给xhr。有什么办法吗?
  • 您的代码中没有任何意义,所以我不知道您真正想要做什么,但您可以在 catch 块中说 return new HttpStatusCodeResult(400, "your error message");(400 表示错误请求)

标签: jquery ajax asp.net-mvc asp.net-mvc-5


【解决方案1】:

您可以做的基本上是创建一个响应模型,如下所示

public class ResponseResult
{
    public bool Success { get; set; }
    public object Data { get; set; }
    public string Message { get; set; }
}

在你的控制器中绑定这个模型如下

[HttpPost]
public JsonResult GetNews(List<NewsModel> newsModelList)
{
  ResponseResult result = new ResponseResult();
  try
  {
        result.Data = //Some Data
        result.Success = true;
        result.Message = "Everything Works Fine!!!";
        return JsonConvert.SerializeObject(result);
  }
  catch (Exception ex)
  {
    //Set Success = false as there is an exception as well as you can pass a message 
    result.Message = "Somethinng Goes Wrong!!!";
    result.Success = false;
    return  JsonConvert.SerializeObject(result);
  }
}

在您的 Ajax 调用中

function ConNews(){
    $.ajax({
        url: $("#getnewsdtl").val(),
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(newsModelLists),
        dataType: "json",
        success: function (result) {
         //check here if result.Success is true or false and than some additional work you can done here
         console.log(result);
        },
         error: function (jqXHR, textStatus, errorThrown) 
        {
        // You will not get exception here because your Ajax call has been called without any errors if it fails in case of 404 or something wrong in your ajax requeste than you will get exception here
        // I want get controller catched exception here.
        }
});

【讨论】:

    【解决方案2】:

    正如评论中提到的,您在catch 块中发送有效数据。所以试试ajax.complete,添加bool来检查是否发生异常

    var checkIfException = false;
    function ConNews(){
        $.ajax({
            url: $("#getFlightPriceIteration").val(),
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify(ticketModelLists),
            dataType: "json",
            success: function (data) {
            checkIfException = true; //setting true
    // some code here.
    },
     error: function (jqXHR, textStatus, errorThrown) 
    {
    
    // I want get controller catched exception here.
    },
     complete: function(){
                if(!checkIfException){
                     alert('Exception occur');
                }
            }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 2012-11-18
      • 1970-01-01
      • 2023-04-01
      • 2022-11-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多