i cannot find any specific issue in google so better post it here, hope there is some solutions. for more than a week, i am facing this issue. I have a function in my web controller like this
public JsonResult DeleteAccount(AccountDeletion accountDeletion) { string msg, sts, is_success; try { var response = new ApiResponse(); var reqDTO = new ChangePassword { email = accountDeletion.email, currentPassword = accountDeletion.password, username = accountDeletion.username }; response = _client.PutApiResponse<ApiResponseConfirmPassword>($"{url}/api/Authentication/submit-delete-account", reqDTO).Result; sts = response.StatusCode; msg = response.messsage; is_success = response.IsSuccess; } catch (Exception e) { msg = e.Message; sts = "fail"; is_success = "false"; } return Json(new { data = "", status = "200", message = msg, success = "false" }); }
this function is being called by ajax request like this
$.ajax({ url: "/AccountDeletion/DeleteAccount", type: 'POST', data: { 'email': $('#email').val(), 'password': $('#password').val(), 'username': $('#username').val() }, success: function (respond) { //console.log(response); if (respond.success === "true") { ////some code } else { /////some code }, error: function (jqXHR, textStatus, errorThrown) { } } });
it always return html code (login.cshtml) when i debug on browser, but when i debug on controller it returns correct json format
i tried commenting the line with api request, and it returns the correct format, but when put it back again, it returns html code again
response = _client.PutApiResponse<ApiResponseConfirmPassword>($"{url}/api/Authentication/submit-delete-account", reqDTO).Result;
, the issue will disappear,right? Just like what @Aaron said, your request to the api is recognized as an authorized so that it redirects your request to the login page, and that's why you get the login page html as the response... You'd better to contact with the developer working on the API and ask him how you can visit that API in asp.net core mvc app, maybe you need to add requset header with a token?