javascript - Valid JSON data cannot be parsed -


i'm new javascript , stuck strange problem. when try parse json data variable, variable becomes undefined.

my code:

document.getelementbyid('searchplayerform').onsubmit = searchplayer;          function searchplayer() {             var request = new xmlhttprequest();              request.open(                 "get", '${url}' + '/' + document.getelementbyid('playerid').value, true             );              request.setrequestheader('accept', 'application/json');             request.onload = responseavailable;              request.send();              return false;          }         function responseavailable() {             switch (this.status) {              case 200: alert(this.responsetext);  var playerresource = json.parse(this.responsetext); alert(playerresource.name); ... 

the first alert returns:

{"playerid":49,"lastname":"xxx","firstname":"walter","address":{"street":"xxx 2","city":"eisden","postalcode":"3630"},"mobilephone":"xxxx/xx.xx.87","phone":"xxx/xx.xx.39","email":"xxx.xxx@live.be","birthdate":"1948-10-11","rank":"d2","highestrank":"c6","membersince":"1981","clubchampion":"1996","function":"materiaalmeester","captain":"","linkvttl":"#http://competitie.vttl.be/index.php?menu=6&season=15&sel=14501&result=1&category=1#","currentmember":true,"currentindex":10,"compno":"507823"} 

the second alert returns "undefined".

anyone has clue might problem here?

thanks in advance!

diher

there no property name in json

there firstname, , lastname

to full name, concat playerresource.firstname , playerresource.lastname

var name = playerresource.firstname + ' ' + playerresource.lastname; alert(name); 

to prevent issues in future, can console.log(playerresource) check data have. or paste jsonlint


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -