php - Parsing string to json/XML from a HttpRequest -
i'm trying parse string nosql database. difficulty. when access php file gives result this:
[{"timestamp":"2016-11-07t09:48:30.335z","id":"d7ee735f16b5"}, {"timestamp":"2016-11-07t09:48:29.015z","id":"d7ee735f16b5"}, {"timestamp":"2016-11-07t09:48:27.688z","id":"d7ee735f16b5"}, {"timestamp":"2016-11-07t09:48:27.085z","id":"d7ee735f16b5"}, {"timestamp":"2016-11-07t09:48:26.577z","id":"d7ee735f16b5"}]
the same result given in network of console.
when attempt stringify response shows null.
please can me access timestamp values. here current code:
var ajax = new xmlhttprequest(); ajax.open("get", url, true); ajax.responsetype = 'json'; ajax.send(); var jsonresponse = json.stringify(ajax.response); document.getelementbyid('demo').innerhtml = jsonresponse;
most response hasn't returned server yet in example. verify element id of 'demo' in fact exist in html document.
add event listeners ajax object:
ajax.onload = loadcompleted; ajax.onerror = onloaderror;
then create function handle result:
var jsonresponse = json.stringify(ajax.response); document.getelementbyid('demo').innerhtml = jsonresponse;
full example (updated per bozidar's comments):
var url = 'https://jsonplaceholder.typicode.com/posts/1'; function loadcompleted() { console.log(ajax.response) var jsonresponse = json.stringify(ajax.response); document.getelementbyid('demo').innerhtml = jsonresponse; } function onloaderror() { //handle error logic } var ajax = new xmlhttprequest(); ajax.onload = loadcompleted; ajax.onerror = onloaderror; ajax.open("get", url, true); ajax.responsetype = 'json'; ajax.send();
see https://jsfiddle.net/px3mxa4n/ working example.
Comments
Post a Comment