Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

0

Add the done function to the end of Ajax

$.ajax({
    .
    .
    .
}).done(function( response ) {
   alert(response);
    ...
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda