Javascript vanilla ajax turn response into array of objects? -


i trying experiment javascript on deeper level. building own $http object has own http methods.

var $http = {      get: function(url, success, error) {         httphelper('get', url, success, error);     }  };  function httphelper(type, url, success, error) {     var xmlhttp;     xmlhttp = new xmlhttprequest();      xmlhttp.onreadystatechange = function() {         if (xmlhttp.readystate == xmlhttprequest.done ) {             if(xmlhttp.status == 200){                 success(xmlhttp.responsetext);             }             else if(xmlhttp.status == 400) {                 error(xmlhttp.status);             }             else {                 error(xmlhttp.status);             }         }     }      xmlhttp.open(type, url, true);     xmlhttp.send(); }; 

on server returning array of json objects request.

app.get('/api/players/', function(req, res) {   res.json([     { name: 'mike', points: 33 },      { name: 'shaq', points: 16 }   ]); }); 

on client seems getting string [{"name":"mike","points":33},{"name":"shaq","points":16}].

how can convert client side response array of json objects?

just use json.parse

json.parse(xmlhttp.responsetext); 

Comments