2

I make a POST ajax request to perform an action and return a result without reloading the page.
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.
For example when an exception is raised, the exception is caught, but his message is null.

The same thing happens when no exception is thrown, "actionResult" is null.
If I use an action that takes less time, the returned messages are not null or empty.
And it doesn't happen in debug mode, the message returned to my page is not 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({
    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 : dotnetfiddle.net/cG2L0u I use Sagnalrac example with a 10 sec wait, and my problem occurred. The list of items is no longer displayed and the console.log returns an empty string. I'm using ASP MVC 5 with .NET 4.8

Tanguy
  • 21
  • 3
  • It sounds like you are exceeding the request timeout for the AJAX request on the client side - this is when it stops as the request has taken too long and you wouldn't have a request body (the code will continue to run on the server in your example). Is this what is happening? – Paddy Aug 19 '20 at 09:16
  • What request is actually sent by browser? And if you set a breakpoint in controller is it hit? – Roman Aug 19 '20 at 09:19
  • This might be useful in figuring that out: https://stackoverflow.com/questions/3543683/determine-if-ajax-error-is-a-timeout – Paddy Aug 19 '20 at 09:19
  • make a breakpoint in line where your post method begins and watch if id is comming from frond-end –  Aug 19 '20 at 09:19
  • Thanks you for your answer. I've tried it with a breakpoint, it works very well. I retrieve the id, and the success or exception messages are returned to my client. I'll try to see if I can get any leads on the ajax timeout. – Tanguy Aug 19 '20 at 09:25
  • I don't think it is about ajax timeout. I try to decrease it to 100 ms an error occurs with statustext = timeout but when i increase it to 10000ms nothing change. – Tanguy Aug 19 '20 at 09:49
  • At least I can tell you you're Ajax request is working fine **[example](https://dotnetfiddle.net/g6HlET)** – Sagnalrac Aug 19 '20 at 11:28
  • @Sagnalrac thanks you for your answer, i didn't know a "jsfiddle" like exist for .NET. I think my problem has to do with the fact that my method takes too long to execute. Using your example, I added a 10 sec wait, and my problem occurred. The list of items is no longer displayed and the console.log returns an empty string. https://dotnetfiddle.net/cG2L0u – Tanguy Aug 19 '20 at 12:21
  • @Tanguy: Would you mind sharing the code that you added to include that 10 secs waintig, please? – Sagnalrac Aug 20 '20 at 03:25
  • @Sagnalrac i added the example URL in the main post. It's this example : https://dotnetfiddle.net/cG2L0u – Tanguy Aug 20 '20 at 06:06
  • @Tanguy: OPS! Sorry! I didn't see that. Thanks! – Sagnalrac Aug 20 '20 at 06:11
  • @Tanguy: Have you tried to make Ajax call synchronous by setting "async: false" in Ajax call parameters? – Harish Aug 21 '20 at 07:09

1 Answers1

0

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).

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.

Once I fixed the problem, I tried to add a timeout of 2 minutes, and no problem occurred (I think in the example, dotnetfiddle has a different configuration than mine, that's why no exception shows up).

Tanguy
  • 21
  • 3