即使在OnResultExecuted成功调用后,JsonResult也没有返回

时间:2012-02-23 13:22:54

标签: jquery json asp.net-mvc-3

我有一个'Session-Less'控制器,其动作返回JsonResult。我的客户端Javascript使用Jquery ajax通过HTTP GET调用此操作

[HttpGet]
public JsonResult Index()
{
    .... 
    // myResult has three properties of type bool, int and a string

    var jsonResult = Json(myResult, "application/json", JsonRequestBehavior.AllowGet);
    return jsonResult;
}

此代码已执行,我已验证调用OnResultExecuted时没有任何异常。

客户端JS

xhrNotification = $.ajax({
            url: '/MyController/', // default action
            type: 'get',
            dataType: 'json',
            cache: false,
            async: true,
            success: function (n, timeout, message) {

            },
            error: function (data) {

            }
        });

但jsonResult只是偶尔会到达客户端。我一直在使用Fiddler观察请求/响应。即使执行了我的操作,HTTP请求也没有响应。我是在Visual Studio 2010下开发的。

感谢任何帮助。

编辑:在进一步调试时,我发现执行Action后出现异常

System.Net.Sockets.SocketException occurred
  Message=An established connection was aborted by the software in your host machine
  Source=System
  ErrorCode=10053
  NativeErrorCode=10053
  StackTrace:
       at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)

部分堆栈跟踪如下

System.dll!System.Net.Sockets.Socket.Send(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) + 0x5a bytes 
    WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Connection.WriteBody(byte[] data, int offset, int length) + 0x40 bytes  
    [Appdomain Transition]  
>   WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Request.FlushResponse(bool finalFlush) + 0x128 bytes    
    System.Web.dll!System.Web.HttpResponse.Flush(bool finalFlush) + 0x4cb bytes 
    System.Web.dll!System.Web.HttpRuntime.FinishRequest(System.Web.HttpWorkerRequest wr, System.Web.HttpContext context, System.Exception e) + 0x80 bytes   
    System.Web.dll!System.Web.HttpRuntime.OnHandlerCompletion(System.IAsyncResult ar) + 0xa6 bytes  
    System.Web.dll!System.Web.HttpAsyncResult.Complete(bool synchronous, object result, System.Exception error, System.Web.RequestNotificationStatus status) + 0x3e bytes   
    System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x25e bytes  
    System.Web.dll!System.Web.HttpApplication.ResumeStepsFromThreadPoolThread(System.Exception error) + 0x28 bytes  
    System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(System.IAsyncResult ar) + 0x183 bytes   
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncUtil.WrapCallbackForSynchronizedExecution.AnonymousMethod__1() + 0x14 bytes    
    System.Web.Mvc.dll!System.Web.Mvc.Async.SynchronizationContextUtil.Sync.AnonymousMethod__3() + 0x16 bytes   
    System.Web.Mvc.dll!System.Web.Mvc.Async.SynchronizationContextUtil.Sync<System.Web.Mvc.Async.AsyncVoid>.AnonymousMethod__0(object o) + 0x32 bytes   
    System.Web.dll!System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(System.Threading.SendOrPostCallback callback, object state) + 0x4a bytes   
    System.Web.dll!System.Web.AspNetSynchronizationContext.CallCallback(System.Threading.SendOrPostCallback callback, object state) + 0x5f bytes    
    System.Web.dll!System.Web.AspNetSynchronizationContext.Send(System.Threading.SendOrPostCallback callback, object state) + 0x9 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.Async.SynchronizationContextUtil.Sync<System.Web.Mvc.Async.AsyncVoid>(System.Threading.SynchronizationContext syncContext, System.Func<System.Web.Mvc.Async.AsyncVoid> func) + 0x58 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.Async.SynchronizationContextUtil.Sync(System.Threading.SynchronizationContext syncContext, System.Action action) + 0x46 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncUtil.WrapCallbackForSynchronizedExecution.AnonymousMethod__0(System.IAsyncResult asyncResult) + 0x6f bytes 
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.ExecuteAsynchronousCallback(bool timedOut) + 0x37 bytes   
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.HandleAsynchronousCompletion(System.IAsyncResult asyncResult) + 0x1e bytes    
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.ExecuteAsynchronousCallback(bool timedOut) + 0x37 bytes   
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.HandleAsynchronousCompletion(System.IAsyncResult asyncResult) + 0x1e bytes    
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.ExecuteAsynchronousCallback(bool timedOut) + 0x37 bytes   
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<System.Web.Mvc.Async.AsyncVoid>.HandleAsynchronousCompletion(System.IAsyncResult asyncResult) + 0x1e bytes    
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<bool>.ExecuteAsynchronousCallback(bool timedOut) + 0x37 bytes 
    System.Web.Mvc.dll!System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<bool>.HandleAsynchronousCompletion(System.IAsyncResult asyncResult) + 0x1e bytes  

1 个答案:

答案 0 :(得分:0)

尝试使用此调用,看看会发生什么:(可能要确保将类型设置为'GET',而不是'get')

$.ajax({
        type: "GET",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: '/MyController/',
        success: function (data) {

        },
        error: function(e) {
        },
        complete: function () {

        }
    });

此外,您可能只想在控制器中执行此操作:

return Json(myResult, JsonRequestBehavior.AllowGet);