content01
content01

Reputation: 3225

How to make a simple ajax call with Jquery and ASP .Net 4

I'm trying the following:

$("#button_feedback").click(function () {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: $('#button_feedback').data('url'),
        data: "{message: " + $('#feedback_content_wrapper textarea').val() + " }",
        dataType: "json",
        success: function (data) {
            alert(data);
        }
    });
});

<input type="button" value="Enviar" class="button" id="button_feedback" data-url="@Url.Action("SendMail", "Contacto")"/>

And in my controller:

public class ContactoController : Controller
    {
        ...

        public ActionResult Send_Mail(string message)
        {
            return Json(new { mensaje = "Correo enviado con éxito" });
        }
}

However I keep getting:

POST http://localhost:11280/Contacto/SendMail404 Not Found

Why? Everything seems correct!!!...

Upvotes: 0

Views: 88

Answers (2)

Adam Rackis
Adam Rackis

Reputation: 83358

After you fix the url issue, you'll want to change how you deal with the json in your callback.

You want to alert the mensaje property on your JSON result alert (data.mensaje);

Your current callback

success: function (data) {
   alert(data);
}

Will just show [object Object]

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532435

Your method name has an underscore. Either change your method name or modify the action in the UrlHelper to match with the underscore.

public ActionResult SendMail(string message) // note change from Send_Mail
{
    return Json(new { mensaje = "Correo enviado con éxito" });
}

Upvotes: 1

Related Questions