0

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:

enter image description here

4 Answers 4

7

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)
});

Sign up to request clarification or add additional context in comments.

Comments

1

Try this one:

var obj = [{"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"}];

console.log( obj[0].PNAME );

var obj = [{"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"}];

console.log( obj[0].PNAME );

Explanation: As you are having an array of object so you have to specify the index and key to access any element.

Comments

0

The data from http://mypath/PP/cfcs/remote.cfc is stringified array of objects so you need to access the object using its array index value. So, when you do

var t = JSON.parse(data);
alert(t.PMNAME);

The value of t is an array so you probably need to loop through t to get all the object or use the index value 0,1, ..., t.length-1 to get the specific object.

Thus, you need to do alert(t[0].PMNAME);

Comments

0

Array of object is returned. So to get a first element of array you should use:

alert(t[0].PMNAME);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.