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
jsonpCallbackparameter is for. That should be the name of the callback function that the JSON is wrapped in. It's not a "filter" function or anything. It's what jQuery uses as your "success" param for JSONP.dataj.AssetGUIDanddataj.AwayForRepair207.230.229.209? Is your code running on that same server? JSONP is something that the server needs to support. It doesn't magically let you get arbitrary JSON files. Are you sure this service even supports JSONP (or CORS)?