即使在OnResultExecuted调用成功后,JsonResult也不会返回
问题描述:
我有一个'Session-Less'控制器,其中有一个返回JsonResult的操作。我的客户端的JavaScript用了jQuery AJAX通过HTTP GET即使在OnResultExecuted调用成功后,JsonResult也不会返回
[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下进行开发。
任何帮助表示赞赏。
编辑:在进一步的调试,我发现我的动作被执行
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
答
尝试使用这种电话,看看会发生什么:(可能想确保你将类型设置为'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);
您能发布客户端代码吗? – 2012-02-23 13:24:22
@SergeyKudriavtsev使用Jquery Ajax代码更新了我的文章。 – byte 2012-02-23 13:29:00
@ b1naryj。不,不得不允许得到。 '返回Json(myResult,JsonRequestBehavior.AllowGet)' – gdoron 2012-02-23 13:34:24