0

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
5
  • 1
    That's not what the jsonpCallback parameter 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. Commented Jun 4, 2014 at 17:21
  • how abt dataj.AssetGUID and dataj.AwayForRepair Commented Jun 4, 2014 at 17:23
  • Do you own 207.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)? Commented Jun 4, 2014 at 17:24
  • Code runs on the same server and it supports jsonp. Commented Jun 4, 2014 at 17:26
  • If the code runs on the same server, then you don't need JSONP. JSONP is only for cross-domain requests. Commented Jun 4, 2014 at 17:26

1 Answer 1

1

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");
}
Sign up to request clarification or add additional context in comments.

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.