forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.ajaxJSON.js
More file actions
166 lines (159 loc) · 5.35 KB
/
Copy pathjquery.ajaxJSON.js
File metadata and controls
166 lines (159 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright (C) 2011 - 2012 Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*jshint evil:true*/
define([
'INST' /* INST */,
'jquery' /* $ */,
'compiled/behaviors/authenticity_token'
], function(INST, $, authenticity_token) {
var _getJSON = $.getJSON;
$.getJSON = function(url, data, callback) {
var xhr = _getJSON.apply($, arguments);
$.ajaxJSON.storeRequest(xhr, url, 'GET', data);
return xhr;
};
// Wrapper for default $.ajax behavior. On error will call
// the default error method if no error method is provided.
$.ajaxJSON = function(url, submit_type, data, success, error, options) {
data = data || {};
if(!url && error) {
error(null, null, "URL required for requests", null);
return;
}
url = url || ".";
if(
submit_type != "GET" &&
// if it's a json request and has already been JSON.stringify'ed,
// then we can't attach properties to `data` since it's already a string
typeof data !== 'string'
) {
data._method = submit_type;
submit_type = "POST";
data.authenticity_token = authenticity_token();
}
var ajaxError = function(xhr, textStatus, errorThrown) {
var data = xhr;
if(xhr.responseText) {
var text = xhr.responseText.replace(/(<([^>]+)>)/ig,"");
data = { message: text };
try {
data = $.parseJSON(xhr.responseText);
} catch(e) { }
}
if(options && options.skipDefaultError) {
$.ajaxJSON.ignoredXHRs.push(xhr);
}
if(error && $.isFunction(error)) {
error(data, xhr, textStatus, errorThrown);
} else {
$.ajaxJSON.unhandledXHRs.push(xhr);
}
};
var params = {
url: url,
dataType: "json",
type: submit_type,
success: function(data, textStatus, xhr) {
data = data || {};
var page_view_update_url = null;
if(xhr && xhr.getResponseHeader && (page_view_update_url = xhr.getResponseHeader("X-Canvas-Page-View-Update-Url"))) {
setTimeout(function() {
$(document).triggerHandler('page_view_update_url_received', page_view_update_url);
}, 50);
}
if(!data.length && data.errors) {
ajaxError(data.errors, null, "");
if(!options || !options.skipDefaultError) {
$.fn.defaultAjaxError.func.call($.fn.defaultAjaxError.object, null, data, "0", data.errors);
} else {
$.ajaxJSON.ignoredXHRs.push(xhr);
}
} else if(success && $.isFunction(success)) {
success(data, xhr);
}
},
error: function(xhr) {
ajaxError.apply(this, arguments);
},
complete: function(xhr) {
},
data: data
};
if(options && options.timeout) {
params.timeout = options.timeout;
}
if(options && options.contentType) {
params.contentType = options.contentType;
}
var xhr = $.ajax(params);
$.ajaxJSON.storeRequest(xhr, url, submit_type, data);
return xhr;
};
$.ajaxJSON.unhandledXHRs = [];
$.ajaxJSON.ignoredXHRs = [];
$.ajaxJSON.passedRequests = [];
$.ajaxJSON.storeRequest = function(xhr, url, submit_type, data) {
$.ajaxJSON.passedRequests.push({
xhr: xhr,
url: url,
submit_type: submit_type,
data: data
});
};
$.ajaxJSON.findRequest = function(xhr) {
var requests = $.ajaxJSON.passedRequests;
for(var idx in requests) {
if(requests[idx] && requests[idx].xhr == xhr) {
return requests[idx];
}
}
return null;
};
$.ajaxJSON.isUnauthenticated = function(xhr) {
if (xhr.status != 401) {
return false;
}
var json_data;
try {
json_data = $.parseJSON(xhr.responseText);
} catch(e) {}
return !!json_data && json_data.status == 'unauthenticated';
};
// Defines a default error for all ajax requests. Will always be called
// in the development environment, and as a last-ditch error catching
// otherwise. See "ajax_errors.js"
$.fn.defaultAjaxError = function(func) {
$.fn.defaultAjaxError.object = this;
$.fn.defaultAjaxError.func = function(event, request, settings, error) {
var inProduction = (INST.environment == "production");
var unhandled = ($.inArray(request, $.ajaxJSON.unhandledXHRs) != -1);
var ignore = ($.inArray(request, $.ajaxJSON.ignoredXHRs) != -1);
if((!inProduction || unhandled || $.ajaxJSON.isUnauthenticated(request)) && !ignore) {
$.ajaxJSON.unhandledXHRs = $.grep($.ajaxJSON.unhandledXHRs, function(xhr, i) {
return xhr != request;
});
var debugOnly = false;
if(!unhandled) {
debugOnly = true;
}
func.call(this, event, request, settings, error, debugOnly);
}
};
this.ajaxError($.fn.defaultAjaxError.func);
};
});