Reputation: 355
I'm trying to loop through a json object and output the value but the value keeps returning as "undefined". What am I doing wrong?
JSON
{"AssetGUID":"00000000-0000-0000-0000-000000000000","AwayForRepair":false}
JavaScript
function runSync() {
var url = "http://207.230.229.209/tag/assettags.json";
$.ajax({
type: "GET",
url: url,
success: successHandlerRunSync,
error: errorHandlerRunSync,
dataType: "json",
jsonpCallback: 'parseJSON' // specify the callback name if you're hard-coding it
});
$('#jsonMsg').html('Running...');
$('#jsonRslt').html(' ');
}
function successHandlerRunSync(data, textStatus, jqXHR) {
var dataJSON = JSON.stringify(data);
$('#jsonMsg').html("RunSync Success <br>status: " + textStatus);
$('#jsonRslt').html(dataJSON);
var content = '';
var dataj = $.parseJSON(data);
$.each(dataj, function(i, post) {
content += '<li>' + post.AssetGUID + '</li>';
content += '<li>' + post.AwayForRepair+ '</li>';
});
$(content).appendTo("#addJSON");
console.log("RunSync Success");
console.log(data);
console.log(dataJSON);
console.log(textStatus);
}
Output
AssetGuid : undefined
AwayForRepair : undefined
Upvotes: 0
Views: 69
Reputation: 227190
You said your code runs on the same server, so get rid of the jsonpCallback
parameter. You're not using JSONP here, it's only for cross-domain requests.
var url = "http://207.230.229.209/tag/assettags.json";
$.ajax({
type: "GET",
url: url,
success: successHandlerRunSync,
error: errorHandlerRunSync,
dataType: "json"
});
And then, in your callback, the JSON will already be parsed for you, no need to call $.parseJSON
.
function successHandlerRunSync(data, textStatus, jqXHR) {
var content = '';
$.each(data, function(i, post) {
content += '<li>' + post.AssetGUID + '</li>';
content += '<li>' + post.AwayForRepair+ '</li>';
});
$(content).appendTo("#addJSON");
}
Upvotes: 1