Last active
January 22, 2016 17:23
-
-
Save Kruithne/06787f93a7595b46c8a4 to your computer and use it in GitHub Desktop.
Tiny ickle filter for remote endpoint services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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