简体   繁体   English

C# 发布 AJAX 空 json 结果

[英]C# Post AJAX empty json result

I make a POST ajax request to perform an action and return a result without reloading the page.我发出 POST ajax 请求以执行操作并返回结果而无需重新加载页面。
For this I use a JSON Result, the problem is that when my action "foo.DoSyncAction(id)" takes a while to execute, the messages returned are null.为此,我使用 JSON 结果,问题是当我的操作“foo.DoSyncAction(id)”需要一段时间才能执行时,返回的消息是 null。
For example when an exception is raised, the exception is caught, but his message is null.比如抛出异常,异常被捕获,但是他的消息是null。

The same thing happens when no exception is thrown, "actionResult" is null.没有抛出异常时也会发生同样的事情,“actionResult”为 null。
If I use an action that takes less time, the returned messages are not null or empty.如果我使用花费更少时间的操作,则返回的消息不是 null 或空的。
And it doesn't happen in debug mode, the message returned to my page is not null.而且在调试模式下不会发生,返回到我页面的消息不是null。
I think I forgot or I'm doing something wrong, but I can't figure out what is wrong.我想我忘记了或者我做错了什么,但我无法弄清楚哪里出了问题。

[HttpPost]
public JsonResult SomeAction(int id)
{
    try
    {
        Foo foo = new Foo();
        var actionResult = foo.DoSyncAction(id);

        return Json(new { ok = true, data = actionResult }, JsonRequestBehavior.AllowGet); ;
    }
    catch (Exception ex)
    {

        return Json(new { data = ex == null ? "null ex" : (string.IsNullOrEmpty(ex.Message) ? "this exception is empty" : ex.Message) }, JsonRequestBehavior.AllowGet);
    }
}

My AJAX query:我的AJAX查询:

$.ajax({
    url: '@Url.Action("SomeAction", "MyController")',
    dataType: 'json',
    data: { id: myID },
    type: 'POST',
    success: function (response) {
        if (response.ok) {
            // When everything is ok
            console.log(response.data);
        } else {
            // When an exception is thrown
            console.log("An error occurred :  " + response.data);

        }
    },
    error: function (error) {
        console.log("An error occurred :  " + error.responseText);
    }
});

Here is an example of a similar situation: do.netfiddle.net/cG2L0u I use Sagnalrac example with a 10 sec wait, and my problem occurred.这是类似情况的示例: do.netfiddle.net/cG2L0u我使用 Sagnalrac 示例等待 10 秒,然后出现了问题。 The list of items is no longer displayed and the console.log returns an empty string.项目列表不再显示,并且 console.log 返回一个空字符串。 I'm using ASP MVC 5 with .NET 4.8我正在使用 ASP MVC 5 和 .NET 4.8

I solved my problem, but I still have some misunderstandings.我解决了我的问题,但我仍然有一些误解。

My method returns an exception systematically related to the account executing the method (that's why in debug it works, whereas on a web server it doesn't, because the accounts used are different).我的方法返回一个与执行该方法的帐户系统相关的异常(这就是为什么在调试中它可以工作,而在 web 服务器上它没有,因为使用的帐户不同)。

The weird thing is that my method does throw an exception, I even manage to catch it and send it by mail.奇怪的是我的方法确实抛出异常,我什至设法捕获它并通过邮件发送。 But when the controller catches the exception, she is empty or null. I'll try to take a closer look at this problem.但是当controller捕获到异常时,她是空的还是null。我会尝试仔细研究这个问题。

Once I fixed the problem, I tried to add a timeout of 2 minutes, and no problem occurred (I think in the example, do.netfiddle has a different configuration than mine, that's why no exception shows up).解决问题后,我尝试添加 2 分钟的超时,但没有出现问题(我认为在示例中,do.netfiddle 的配置与我的不同,这就是没有出现异常的原因)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM