0

Intente Diferentes formas de llamadas, mismos nombres, con stringify, sin stringify.... pero nada resulta.

mi html

    <input type="text" id="txtName"/>
    <input type="button" id="btnGet" value="Get Current Time"/>
    <input type="text" id="txtresponse"/>

mi jscript

$(function () {
            $("#btnGet").click(function () {
                $.ajax({
                    type: "POST",
                    url: "/Home/AjaxMethod",
                    data: '{name: "' + $("#txtName").val() + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        //alert(response);
                        $("#txtresponse").val(response);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        //alert(response.responseText);
                    }
                });
            });
        });

y mi controller es

[HttpPost]
public ContentResult AjaxMethod(string name)
{
    string currentDateTime = string.Format("Hello {0}.\nCurrent DateTime: {1}", 
                                                      name, DateTime.Now.ToString());
    return Content(currentDateTime);
}

Aqui, El controller "AjaxMethod" siempre recibe null como valor del parametro "name"

Mi Version es .Net 2022 y .Net 6

Muchas Gracias por su ayuda

1 respuesta 1

0

Estas creando el parámetro data como un string, y no como un json. Intenta sin las comillas:

$(function () {
    $("#btnGet").click(function () {
        $.ajax({
            type: "POST",
            url: "/Home/AjaxMethod",
            data: {name: "' + $("#txtName").val() + '" },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            ...

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.