Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
La devolución de llamada de AJAX no muestra el mensaje de éxito - ASP.NET MVC C#

Tengo un código AJAX en mi JavaScript que no muestra ninguna alerta de éxito o falla.

 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); } }); };

La función anterior se llama cuando hago clic en una imagen en la pantalla. Esta parte funciona bien ya que he establecido un punto de interrupción en mi controlador ASP y puedo ver que se llama a la acción relevante. Código C# a continuación:

 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!" }); } }

Aunque las líneas Json de retorno se golpean y se devuelven a la página, no hay una ventana emergente de alerta en mi página para que el usuario sepa si tuvo éxito o no. Por favor, hágamelo saber si tiene alguna pregunta.

Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Agregue la función done al final de Ajax

 $.ajax({ . . . }).done(function( response ) { alert(response); ... });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!