Basic jQuery with JSON Parsing
I am trying to parse some very basic JSON, but I don't know where I'm going wrong when trying to display it to the screen.
Am I not GRABBING the data correctly, 开发者_运维问答such as, "data.re1Code"? I hope someone can shed some light onto my basic question sorry.
JSON Data
[
{
"rep1FullName": "Justin Giesbrecht",
"rep1Code": "dc",
}
]
Javascript
$.ajax({
type: "GET",
url: "testJSONData.php",
dataType: "json",
success: function(data) {
$("#output").append(data.rep1FullName);
},
error: function () { alert("Error"); }
}); // End of generated json
The brackets [] make data a JSON array with your object as the 0th element so to get "Justin Giesbrecht" use the code: $("#output").append(data[0].rep1FullName);
or remove the brackets and make the JSON:
{
"rep1FullName": "Justin Giesbrecht",
"rep1Code": "dc",
}
Your data is an array.
So you'd want
$("#output").append(data[0].rep1FullName);
You are returning a jSon array so you would need to access it via data[0].rep1FullName
or return the jSon as below and then use data.rep1FullName
{
"rep1FullName":"Justin Giesbrecht",
"rep1Code":"dc"
}
Also, remove the last comma from the object notation.
[
{
"rep1FullName": "Justin Giesbrecht",
"rep1Code": "dc" // <-- No comma, breaks in IE if you have a comma.
}
]
Some of the other posters did this, but didn't mention it.
精彩评论