用的HTTPStatus codeResult和放大器自定义错误消息; jQuery的自定义、放大器、错误、消息

2023-09-04 00:31:27 作者:永不止步

我有一个控制器动作,返回一些JSON结果jQuery的完整的日历插件。我返回的HTTPStatus codeResult与自定义错误消息,如果有一个错误,但我不能让显示自定义错误消息。所有显示的警告框,这是默认的HTTP状态(即:禁止或内部服务器错误)

控制器code,返回的错误信息

 否则,如果(ID!= CurrentUser.UserId)
{
    返回新的HTTPStatus codeResult(403,您无权查看此。);
}
其他
{
    返回新的HTTPStatus codeResult(500,有在服务器上的错误。);
}
 

jQuery的code

  $(文件)。就绪(函数(){
             $('#日历)。fullCalendar({
                 标题:{
                     左:'preV,接下来的今天,
                     中心:'标题',
                     右:一个月,agendaWeek,agendaDay
                 },
                 高度:600,
                 eventSources:[{
                     网址:事件,
                     输入:'获取',
                     错误:函数(响应状态,错误){
                         警报(response.statusText);
                     }
                 }],
                 allDayDefault:假的,
                 可选:真正的,
                 eventClick:功能(事件){
                     如果(event.url){
                         $('#详细信息)的负载(event.url)。
                     }
                 },
 

解决方案

其实,有没有在code的任何问题都没有。问题是,在Visual Studio中的ASP.NET开发Web服务器。我转向使用IIS EX preSS并能正常工作。

why we see different http status code like 404, 500. where are they handled

I have a controller action that returns some JSON results to the jQuery Full Calendar plugin. I return a HTTPStatusCodeResult with a custom error message if there was an error, But i cant get the custom error message to display. All that's is displayed in the alert box is the default Http status (ie: 'Forbidden', or 'Internal Server Error')

Controller code that returns the error messages

else if(id != CurrentUser.UserId)
{
    return new HttpStatusCodeResult(403, "You are not authorised to view this.");
}
else
{
    return new HttpStatusCodeResult(500, "There was an error on the server.");
}

jQuery code

$(document).ready(function () {
             $('#calendar').fullCalendar({
                 header: {
                     left: 'prev,next today',
                     center: 'title',
                     right: 'month,agendaWeek,agendaDay'
                 },
                 height: 600,
                 eventSources: [{
                     url: events,
                     type: 'Get',
                     error: function(response, status, error) {
                         alert(response.statusText);
                     }
                 }],
                 allDayDefault: false,
                 selectable: true,
                 eventClick: function (event) {
                     if (event.url) {
                         $('#details').load(event.url);
                     }
                 },

解决方案

Actually there wasn't any problem in the code at all. The problem was with the ASP.NET development Web Server in Visual Studio. I switched to using IIS express and it works fine.