Wednesday, 7 June 2017

get serializearray() elements with ajax and node js

I would like to get input elements from my form to node js:

Client Side

var ss =( $('#addform').serializeArray() );
var url= 'http://localhost:8181/add';
    $.ajax({
        url:url,
        type: 'POST',
        dataType: "json",
        data:{ss:ss},
        success: function(json){
            alert("done");          
    },
    error: function(jqXHR, textStatus, err) {
        alert('text status '+textStatus+', err '+err)
    }
});

[Object, Object, Object, Object, Object, Object, Object]

Server Side:

router.route('/adduser')
.post(function(req, res) {
  res.header("Access-Control-Allow-Origin", "*");
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      console.log(req.body);
      console.log(req.body.name);
}

Result:

{ ss: 
   [ { name: 'name', value: 'a' },
     { name: 'lastname', value: 'a' },
     { name: 'email', value: 'a' },
     { name: 'password', value: 'a' },
     { name: 'password2', value: 'a' },
     { name: 'country', value: 'a' },
     { name: 'phone', value: 'a' } ] }

console.log(req.body.name); returns undefined

How can I get elements of these objects in server side ?



via ahmed

No comments:

Post a Comment