I have a text and a button:
<div id="divtest"> <h1> Bla </h1> </div>
<button id="dugme1"> dugme </button>
When the user clicks the button, this script is called:
$("#dugme1").click(function(){
$.get("http://mypath/PP/cfcs/remote.cfc", {
method: 'listProjects',
returnformat: 'json'
},function(data){
console.log(data);
var t = JSON.parse(data);
alert(t.PMNAME);
})
});
So I get this as a response in the console when I click a button:
[{"PNAME":"JumpingBunny","ANAME":"Nikola","PID":1,"PMNAME":"Marko"},{"PNAME":"WorkerLogger","ANAME":"Ivan","PID":2,"PMNAME":""},{"PNAME":"TimeStampter","ANAME":"Kevin","PID":3,"PMNAME":"Bart"},{"PNAME":"BugFixer2","ANAME":"Ivan","PID":4,"PMNAME":"Clark"},{"PNAME":"EditorS","ANAME":"Homer","PID":5,"PMNAME":"Bruce"}]
But I want the alert to show me the value of PNAME. But I get undefined when I click the button.
What am I doing wrong ? This is the developer console:
Since t
is an array, you have to use index like:
t[0].PMNAME
To print all PMNAME
you can use forEach()
on the array:
var t = [{"PNAME":"JumpingBunny","ANAME":"Nikola","PID":1,"PMNAME":"Marko"},{"PNAME":"WorkerLogger","ANAME":"Ivan","PID":2,"PMNAME":""},{"PNAME":"TimeStampter","ANAME":"Kevin","PID":3,"PMNAME":"Bart"},{"PNAME":"BugFixer2","ANAME":"Ivan","PID":4,"PMNAME":"Clark"},{"PNAME":"EditorS","ANAME":"Homer","PID":5,"PMNAME":"Bruce"}];
t.forEach(function(i){
console.log(i.PNAME)
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments