2

I am calling .net core action via ajax request. It's not even waiting for the return statement but as soon as I am calling Auth0 management api it's returning error.

   [HttpPost]
    public async Task<IActionResult> Create([FromBody]ConnectionCreateRequest model)
    {
        try
        {
            var result = await _managementApiClient.Connections.CreateAsync(model);
            return Ok();
        }
        catch (Exception ex)
        {
            return Content(ex.Message);
        }
    }

It's returning error after result statement.

Here is the ajax call:

  $.ajax({
        type: "POST",
        url: '@Url.Action("Create", "Connection")',
        contentType: "application/json charset=utf-8",
        data: JSON.stringify(model),
        success: function (result) {
            alert('Connections has been created successfully');
        },
        error: function (result, err) {
            alert(err);
        },
        complete: function () {
            hideLoadingGif();
        }
    });
});

What am I doing wrong?

10
  • Try data: model, directly... It's returning error after result statement. what the error says? Commented Sep 27, 2018 at 13:52
  • What is the error with the Ajax call? Commented Sep 27, 2018 at 13:53
  • just an alert box which says "error" Commented Sep 27, 2018 at 13:55
  • @ZakariaAcharki, if I pass data: model directly then I am getting null in model argument in my action Commented Sep 27, 2018 at 13:57
  • What is the content of model? Commented Sep 27, 2018 at 13:58

2 Answers 2

4

The problem is that in $.ajax method you didn't specify dataType property. In this case $.ajax makes an "intelligent guess" based on response content type:

dataType (default: Intelligent Guess (xml, json, script, or html)) The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response.

Ok() method in .net core returns empty response without MIME type specified in response header. That's why $.ajax triggers error callback with statusText "parseerror".

The solution is:

  • to set $.ajax dataType option to "text", and $ajax will accept empty reponse and trigger success callback
  • in .net core to return Json(true) or Json(null) for successful response, and $.ajax will automatically recognize it as json response and trigger success callback.

Documentation for $.ajax dataType option for json response:

As of jQuery 1.9, an empty response is also rejected if dataType is json; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also return Ok(true) in .net core for successful response.
0

The problem is that your Controller is returning Content in the exception Which is a 200 status code result in the terms of IAction Result.

Your controller is catching the exception correctly but still returning a 200 because of the Content return Statement.

5 Comments

No, I have a debugger on exception. When there is an exception it call the catch block. And as I said there is no exception, it calls return Ok() statement.
I would mention that in the original question , regardless of this you will still always receive 200 even if an exception is thrown.
@cl0ud you are correct - the js error handler will not pickup errors from with the C#. However, OP is getting the error handler, so this is not the cause (though it might be the next question OP asks...)
@freedomn-m perhaps I misunderstood the question but regardless this confirms that it is impossible for the js error handler to hit in any case as you said.
well, not in any case as OP is (was) getting the error handler - but getting the error handler when there was some other issue (in this case they've specified JSON but not returned JSON so jquery was failing to convert the 200 "(no message)" to JSON and passing to the error handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.