2

In my ASP.NET Core MVC web app project, I'm sending a POST request to an external API using HttpClient. Everything seems to be working correctly, but on the JavaScript side, the response is undefined. There are no error messages either.

Controller:

[HttpPost]
public async Task<Kullanici> KullaniciKayit(Kullanici model)
{
    var result = await _kullaniciService.KullaniciKayit(model);
    return Ok(result); // Bu satır var
}

My Ajax request:

$.ajax({
    url: "/Home/KullaniciKayit",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify(model),
    success: function (data) {
        console.log("Gelen veri:", data); // <-- burada 'undefined' geliyor
        alert("Kayıt başarılı");
    },
    error: function (xhr) {
        console.error("Hata:", xhr.responseText);
    }
});

My micro service:

public async Task<Kullanici> KullaniciKayit(Kullanici model)
{
    var json = JsonConvert.SerializeObject(model);
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    var response = await _httpClient.PostAsync("api/KullaniciKaydet/KullaniciKayit", content);

    if (response.IsSuccessStatusCode)
    {
        var responseBody = await response.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<Kullanici>(responseBody);
    }
    else
    {
        throw new HttpRequestException($"API Error: {response.StatusCode}");
    }
}

Web app: I'm sending an Ajax request from a form to the KullaniciKayit action in the HomeController. This action calls the API via a service layer.

API: the endpoint https://localhost:44397/api/KullaniciKaydet/KullaniciKayit is working properly.

The Kullanici model is shared between both the web app and the API.

What I've checked:

  • The API was tested via Postman → it returns the correct data
  • The web app controller includes return Ok(...)
  • The API returns Content-Type: application/json
  • In the browser's Network tab, the request is successful but the response body is {}
  • console.log(data) shows undefined

What could be the issue?

5
  • the response body is {}...what did you expect it to be, and why? That's potentially a valid response. Have you checked what var responseBody in the C# code contained, while debugging your C#? Commented Jul 2 at 7:21
  • 3
    Maybe try converting public async Task<Kullanici> to public async Task<IActionResult> Commented Jul 2 at 7:22
  • 1
    You can also give your success function a bit more data: success: function (data, status, xhr) { console.log("Status:", status); console.log("Raw Response:", xhr.responseText); console.log("Parsed Data:", data); } Commented Jul 2 at 7:23
  • I surprise that your compiler didn't provide you the compilation error that you are returning the value with OkResult<> type in your KullaniciKayit API action which expects the return value with Task<Kullanici>. Commented Jul 2 at 7:41
  • Just an aside: Why don't you call https://localhost:44397/api/KullaniciKaydet/KullaniciKayit directly from the AJAX code? Your ASP.NET doesn't add any value to the call - it just passes it on and returns the value. Doesn't add or remove or transform data, doesn't handle any extra authentication, or anything else useful. Why not remove the middleman? If you get CORS issues you can solve that in the API code Commented Jul 2 at 8:11

2 Answers 2

1

Do this for KullaniciKayit. You are getting undefined because return Ok(result) returns ActionResult but your result is not

[HttpPost]
public async Task<ActionResult<Kullanici>> KullaniciKayit(Kullanici model)
{
    var result = await _kullaniciService.KullaniciKayit(model);
    return Ok(result);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The issue is resolved, thank you! I really appreciate your quick and clear responses. :)
1

Wrote in the comment, surprise that you are able to build the project without your compiler prompts the compilation error message for the returned value (type) don't meet the return type of your KullaniciKayit API action method.

Approach 1: Change the method signature for the return type: Task<IActionResult>

Credit to @Carsten Løvbo Andersen's comment, your KullaniciKayit API action's return type should change to Task<IActionResult>.

[HttpPost]
public async Task<IActionResult> KullaniciKayit(Kullanici model)
{
    var result = await _kullaniciService.KullaniciKayit(model);
    return Ok(result);
}

Approach 2: Return result as Kullanici without Ok()

You can also return the specific type without the Ok method.

[HttpPost]
public async Task<Kullanici> KullaniciKayit(Kullanici model)
{
    var result = await _kullaniciService.KullaniciKayit(model);
    return result;
}

1 Comment

Thank you so much for your help and quick response! Your suggestions really helped me understand and fix the issue. I appreciate your time and support a lot.

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.