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

382
Vistas
Ajax Post request body returns undefined

I am trying to send some data to backend using ajax post request. Like this

    $.ajax({
    url: "/aa",
    dataType: 'text',
    type: 'post',
    contentType: 'application/x-www-form-urlencoded',
    data: input.value,
    success: function (result) {
        console.log("Success", input.value);
    },
    error: function (data) {
        console.log(data);
    }
});

but i when i want to print my bodys data like this i recieve undefined

console.log(requ.body.data);

i also use body parser in the backend

app.use(bodyParser.raw())

why it returns undefined?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Hope this will work

<script>  
    $(document).ready(function () { 
//function will be called on button click having id btnsave
        $("#btnSave").click(function () { 
            $.ajax(  
            {  
                type: "POST", //HTTP POST Method  
                url: "Home/AddEmployee", // Controller/View   
                data: { //Passing data  
                    Name: $("#txtName").val(), //Reading text box values using Jquery   
                    City: $("#txtAddress").val(),  
                    Address: $("#txtcity").val()  
                }  
  
            });  
  
        });  
    });  
  
</script> 


in controller side

     [HttpPost]    
        public ActionResult AddEmployee(EmpModel obj)    
        {    
            AddDetails(obj);    
      
            return View();    
        }    
over 4 years ago · Santiago Trujillo Denunciar

0

this works, please tell me if you want me to create a fiddle

Controller:

[HttpPost]
public string Index900(string theValue)
{
    try
    {
        throw new Exception("this is the Value" + theValue);
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

public ActionResult Index()
{
    return View();
}        

View:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script>
        $(function () {
            $("#theBtn").click(function () {
                var theValue = $("#theInput").val();
                $.ajax({
                    url: "/Home/Index900",                          //changed this
                    dataType: 'text',
                    type: 'post',
                    contentType: 'application/json',                //changed this
                    data: JSON.stringify({ theValue: theValue }),   //changed this
                    success: function (result) {
                        //console.log("Success", input.value);      //changed this
                        alert(result);
                    },
                    error: function (data) {                        //changed this
                        //console.log(data);                        //changed this
                        alert("error...")
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div>
        <input type="button" id="theBtn" value="Click to start ajax" />
        <input type="text" id="theInput" value="Some Value" />
    </div>
</body>
</html>
over 4 years ago · Santiago Trujillo 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