Skip to content

Instantly share code, notes, and snippets.

@Xennis
Last active July 17, 2020 22:08
Show Gist options
  • Save Xennis/9263633 to your computer and use it in GitHub Desktop.
Save Xennis/9263633 to your computer and use it in GitHub Desktop.
JavaScript jQuery HTTP Client

JavaScript jQuery HTTP Client

Full documentation see: JavaScript - JSON and REST

Used software

Used JavaScript library

Usage

Get example with OAuth

var client = new HttpClient();
client.httpGet("http://example.org/api/message/", "OAuth " + access_token,
    function (data, textStatus, jqXHR) {
        console.log("receive: " + textStatus + " " + jqXHR.status + " " + jqXHR.statusText);

        $.each( data.objects, function( i, item ) {
            // e.g. access attribute name: console.log(item.name)
        });
    },
    function (jqXHR, textStatus) {
        console.log("receive: " + textStatus + " " + jqXHR.status + " " + jqXHR.statusText);
        $("#getResult").text(completeMessage);        
    }
);

Post example with OAuth

var client = new HttpClient();
client.httpPost("http://example.org/api/message/", "OAuth " + access_token, data,
    function (jqXHR, textStatus) {
        console.log("receive: created");
        console.log(textStatus + " " + jqXHR.status + " " + jqXHR.statusText);
    },
    function (jqXHR, textStatus) {
        console.log("receive: " + textStatus + " " + jqXHR.status + " " + jqXHR.statusText);
        console.log(jqXHR.responseText);
    }
);
/*
* Perform an asynchronous HTTP (Ajax) request.
*/
(function ($, undefined) {
window.HttpClient = function() {
}
HttpClient.prototype = {
httpPost: function (url, authorization, data, success_callback, fail_callback) {
return httpPost(url, authorization, data, success_callback, fail_callback);
},
httpGet: function (url, authorization, success_callback, fail_callback) {
return httpGet(url, authorization, success_callback, fail_callback);
},
}
function httpPost(url, authorization, data, success_callback, fail_callback) {
console.log("request:" + url + data);
return $.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: data,
dataType: 'json',
processData: false,
beforeSend: function (xhr) {
if (authorization != "") {
xhr.setRequestHeader('Authorization', authorization);
}
xhr.setRequestHeader('Accept', "application/json");
},
complete: function(jqXHR, textStatus) {
switch (jqXHR.status) {
case 201:
success_callback(jqXHR, textStatus);
break;
default:
if (fail_callback) {
fail_callback(jqXHR, textStatus);
}
break;
}
},
});
}
function httpGet(url, authorization, success_callback, fail_callback) {
console.log("request:" + url);
return $.ajax({
url: url,
dataType: 'json',
beforeSend: function (xhr) {
if (authorization != "") {
xhr.setRequestHeader('Authorization', authorization);
}
xhr.setRequestHeader('Accept', "application/json");
},
success: function(data, textStatus, jqXHR) {
success_callback(data, textStatus, jqXHR);
},
error: function(jqXHR, textStatus, errorThrown) {
if (fail_callback) {
fail_callback(jqXHR, textStatus);
}
},
});
}
})(jQuery, undefined);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment