Friday, 2 June 2017

serving XMLHttp POST request on node js server

Im trying to serve XMLHttprequest (POST) on node js server.

Server side:

 app.use(bodyParser.urlencoded({extended:true}));
    app.use(bodyParser.json());


    app.post('/count', function(request, response){
      console.log(JSON.stringify(request.body));      
      response.send(JSON.stringify(request.body));    
    });

Client side:

<script>
var xmlhttp = new XMLHttpRequest();
var resp = ""
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
        resp = xmlhttp.responseText;
        resp = JSON.parse(resp);
        document.write(resp);
        }
}

var check = "Hello";

function show(){
xmlhttp.open("POST","http://localhost:3000/count",  true);
xmlhttp.send(JSON.stringify(check));
</script>

The output Im getting is [object Object]

Where am i going wrong?



via Black Heart

No comments:

Post a Comment