weixin_33692284 2015-05-26 07:54 采纳率: 0%
浏览 69

返回错误并在ajax中查看

Hello everyone I want to know something is there possible. Using ajax send error message if occurred else send view. you can see my code. please help me what should I do. need some suggestions

here is my example

public ActionResult Test()
{
  string ErrorText = string.Empty;
  if(true)
    return View("PagedList");
  }
  else
  {
    return Json(new { errorMessage = ErrorText});
  }
}

Ajax:-

function Load(page) {

            $.ajax({
                type: "POST",
                url: page,
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {

                        $('#ReportLoad').empty();
                        $('#ReportLoad').append($.parseHTML(data));

                },
                 error: function (xhr, textStatus, exceptionThrown) {
                    $('#ReportErrors').empty();
                    $('#ReportErrors').html(JSON.parse(xhr.responseText));
                    $('#ReportErrors').show();
                },
                complete: function () {
                }
            });
        }

I don't know I am doing right way or not Can anyone suggest whats the best way to achieve this.

  • 写回答

1条回答 默认 最新

  • weixin_33724046 2015-05-26 08:17
    关注

    Return the error code as a HttpStatusCodeResult, if you just send a reply there is no way of Ajax knowing it is an error unless you check for specific text in the response.

     public ActionResult Test()
     {
          string ErrorText = string.Empty;
          if(true)
            return View("PagedList");
          }
          else
          {
            return new HttpStatusCodeResult(500, ErrorText);
          }
     }
    

    And then detect this with Ajax:

    function Load(page) {
                $.ajax({
                    type: "POST",
                    url: page,
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
    
                            $('#ReportLoad').empty();
                            $('#ReportLoad').append($.parseHTML(data));
    
                    },
                     error: function (xhr, textStatus, exceptionThrown) {
                        $('#ReportErrors').empty();
                        $('#ReportErrors').html(JSON.parse(xhr.responseText));
                        $('#ReportErrors').show();
                    },
                    complete: function () {
                    },
                       statusCode: {
                       500: function(response)
                       {
                           alert("internal error");
                       }
                     }
                });
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64