cal
cal

Reputation: 1863

AJAX GET request going to error despite getting the response

I've set up a GET request to retrieve some JSON from an API for my website. It keeps going to an Error despite showing the response correctly in the network tab of the Console and a 200 status (pictures attached). I've run the JSON response through a validator and it checks out fine. What is the problem?

$(document).ready(function() {
    console.log( "ready!" );
    bindProject();
});

function bindProject() {
    $(".project-link").on("click.project", function() {
        console.log("clicked");
        var id = $(this).data("id");
        var $this = $(this);

        loadProject(id, function() {
            $("#previous, #next").show();
        })
    });
}

function loadProject(id, callback) {
    $.get(BASE + "api/page/projects/" + id, {}, function(data) {
        if (data.success == "true") {
            console.log("success");
            $("#project-info").attr("projectid", id);
            $("#project-title").html(data.title);
            $("#project-info").html(data.info);
            $("#project-year").html(data.info);
            $("#project-tags").html(data.tags);
            setProject();
        else {
            console.log("error")
        }
    }, "json")
}

200 STATUS

JSON RESPONSE

Upvotes: 0

Views: 301

Answers (1)

rbock
rbock

Reputation: 625

Does data have an attribute success? If what you're trying to do is check if the connection was successful, you could use:

$.get( BASE + "api/page/projects/" + id, function(data) {
    alert( "success" );
    })
    .done(function(data) {
        alert( "second success" );
        // handle success case here
    })
    .fail(function(data) {
        alert( "error" );
        // handle error case here
    });

More information here. Hope it helps.

Upvotes: 1

Related Questions