locked
again problem with ajax RRS feed

  • Question

  • User-1062853889 posted

    Hello, againg I have problem with ajax, the data is null.

    Example:

     var data = { "name": "panchito" };
                $.ajax({
                    url: '/ajax/GuardarUser',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (err) {
                        alert("Error: " + err.responseText);
                        console.log(err);
                    }
                })

    Controller

    [HttpPost]
            public IActionResult GuardarUser([FromBody]string name)
            {
                var data = name;
                data = data + "ooo";
                var data1 = name;
                return Json(data1);
            }
        }

    the name is null

    If remove this line contentType: 'application/json', there show me this error 415
    If I put this line contentType: "application/json; charset=utf-8" the result is null, the name = null

    Some idea?

    Thursday, May 20, 2021 8:26 PM

All replies

  • User-1062853889 posted

    ok, is working.
    I fix romove this lines

    dataType: 'json',
                    contentType: 'application/json',

    Thursday, May 20, 2021 8:52 PM