Skip to content

Instantly share code, notes, and snippets.

@Kruithne
Last active January 22, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kruithne/06787f93a7595b46c8a4 to your computer and use it in GitHub Desktop.
Save Kruithne/06787f93a7595b46c8a4 to your computer and use it in GitHub Desktop.
Tiny ickle filter for remote endpoint services
$.poke = function(endpoint, parameters, callback)
{
var errorResponse = {
success: false,
error: "Internal Error"
};
var doAjax = false;
var doPost = false;
if (typeof(parameters) == "object")
{
if (parameters instanceof jQuery && parameters.is("form"))
{
doAjax = true;
parameters = new FormData(parameters[0]);
}
else
{
if (typeof(parameters._method) == "string")
{
if (parameters._method.toLowerCase() == "post")
{
doPost = true;
delete parameters._method;
}
}
}
}
var doneFunc = function(result) {
callback((typeof(result) == "object" && typeof(result.success) == "boolean") ? result : errorResponse);
};
var failFunc = function() {
callback(errorResponse);
};
if (doAjax)
{
$.ajax({
url: endpoint,
data: parameters,
processData: false,
contentType: false,
type: 'POST'
}).done(doneFunc).fail(failFunc);
}
else
{
var theFunc = doPost ? $.post : $.get;
theFunc(endpoint, parameters).done(doneFunc).fail(failFunc);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment