-1

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

3
  • 1
    Well if your request to the api endpoint returns the LOGIN page it looks like the user is not authenticated and authorized to use the endpoint and thus you serve the login page?
    – Aaron
    CommentedAug 21, 2024 at 8:12
  • According to your description, when you comment 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?
    – Tiny Wang
    CommentedAug 21, 2024 at 8:19
  • should i put [AllowAnonymous] in the api?CommentedAug 21, 2024 at 9:15

1 Answer 1

-1

So, i know the answer...Basically what i am trying to do is creating a simple form in my website to following google policy of "Account Deletion". A bit troublesome that the account used on my mobile apps is no eligible to access web. so, i simulate a login function in the background before user can request to delete their account. thanks to @Aaron mention, i need to put a token in my request to other api. And my code works

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.