Skip to content

Instantly share code, notes, and snippets.

@adoc
Created January 25, 2014 08:00
Show Gist options
  • Save adoc/8613254 to your computer and use it in GitHub Desktop.
Save adoc/8613254 to your computer and use it in GitHub Desktop.
temp: api call outside of backbone
// api calls outside of backbone.
var apiCall = function(uri, data, success, method) {
data = data || {};
method = method || 'GET';
$.ajax({
method: method,
url: opts.apiRoot + uri,
data: method == 'POST' ? JSON.stringify(data) : data,
dataType: 'json',
beforeSend: function(xhr, options) {
var headers = authApi.send(data);
options.headers = headers;
_.each(headers, function(value, header) {
xhr.setRequestHeader(header, value);
});
},
success: function(data, status, xhr) {
authApi.receive(xhr.responseJSON, xhr.getResponseHeader);
if (success) {
success(data, status, xhr);
}
},
error: function(dunno, xhr, error){
if (dunno.status = 403) {
// Cookies bad as far as the server is concerned (??)
var defaultOpts = default_config();
if (authOpts == defaultOpts)
throw "apiCall: Final auth attempt failed. Nowhere to go from here. :(";
remove_cookies();
authOpts = defaultOpts;
authApi = _.extend(authApi, defaultOpts);
apiCall(uri, data, success, method);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment