Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
AJAX Callback Not Showing Success Message - ASP.NET MVC C#

I have some AJAX code in my JavaScript which is not showing any success or failure alert.

  function AttemptHouseViewingAppointment(house) {

    var imgOfHouse = $(house).attr("value");
    $.ajax({
        type: "POST",
        url: '@Url.Action("AttemptHouseViewingAppointment", "Viewing")',
        dataType: "json",
        data: ({
            userId: @Model.UserId,
            appointmentKey: '@Model.Key',
            chosenHouse: imgOfHouse 
        }),
        success: function (data) {
            alert(data);
            if (data.success) {
                alert(data.message);
            } else { alert(data.Message) }
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });

};

The above function is called when I click an image on the screen. This part works fine as I have set a breakpoint on my ASP controller and I can see the relevant action being called. C# code below:

    public ActionResult AttemptHouseViewingAppointment(int userId, string appointmentKey, int chosenHouse)
    {
        string selecteHouseName = $"./house-code-icons/{chosenHouse}.png";

        var house = 
            _ctx.houses.Where(x => x.HouseID == userId && x.Icon == chosenHouse)
                        .FirstOrDefault() ?? null;

        if(house != null)
        {
            var member = _ctx.User.FirstOrDefault(x => x.Id.Equals(userId));

            _ctx.Appointments.Add(new ViewingModel
            {
                House = chosenHouse,
                UserId = userId
            });

            _ctx.SaveChanges();

            return Json(new { success = true, message = "Appointment Confirmed!" });
        }
        else
        {
            return Json(new { success = false, message = "Sorry, a booking has already been made!" });
        }
    }

Even though, the return Json lines are being hit and returned to the page, there is no alert popup on my page to let user know if success or not. Please let me know if any questions.

Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add the done function to the end of Ajax

$.ajax({
    .
    .
    .
}).done(function( response ) {
   alert(response);
    ...
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda