jQuery AJAX request demo

by JokerMartini

HTML

<p>
Access Token
</p>
<div id='accessOutput' class="output">waiting for action</div>
<p>
Query Results
</p>
<div id='queryOutput' class="output">waiting for action</div>

CSS

a {
    padding: 0.2em;
}
a:hover {
    background-color: #02AAEE;
}
.output {
    display: block;
    margin-top: 8px;
    padding: 1%;
    border: 1px solid #666;
    background-color: #efefef;
    
}

JavaScript

function queryShotgun() {
    var headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    };

    $.ajax({
        url: 'https://studiodashboard.shotgunstudio.com/api/v1/auth/access_token',
        method: 'post',

        headers: headers,
        data: {
            grant_type: "client_credentials",
            client_id: "queryToken",
            client_secret: "(ooiqsnkvy6vsdJnehatxpdjd"
        },
        success: function (data) {
            console.log(JSON.stringify(data));
            console.log(data.access_token);
            $('#accessOutput').html(data.access_token);

            var headers = {
                'Accept': 'application/json',
                'Authorization': 'Bearer ' + data.access_token + ''

            };
            

            $.ajax({
            		url: 'https://studiodashboard.shotgunstudio.com/api/v1/entity/projects',
                // url: 'https://studiodashboard.shotgunstudio.com/api/v1/entity/Task',
                method: 'get',
                headers: headers,
                data: {
                    fields: ['name', 'status', 'code'],
                    filters: [['sg_status', 'is', 'Active']]
                },
                success: function (data) {
                    console.log(JSON.stringify(data));
                    $('#queryOutput').html(response.responseText);
                },
                error: function () {
                    $('#queryOutput').html('Bummer: there was an error!');
                },
            })
        }
    })
}

window.onload = function(){
  queryShotgun();
};