This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
jsonp.js
122 lines (104 loc) · 3.34 KB
/
jsonp.js
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
/*
JSONProxy jQuery Plugin, v0.3.1
https://jsonp.afeld.me
by Aidan Feldman
MIT license
*/
/* eslint-env amd, browser */
/*global jQuery, URI */
(function(factory) {
'use strict';
// https://github.com/umdjs/umd/blob/ce6c20e318e58cd301ee929135cf651b02392c08/jqueryPlugin.js
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([
'jquery',
// https://github.com/medialize/URI.js#requirejs
'URIjs/URI'
], factory);
} else {
// Browser globals
factory(jQuery, URI);
}
})(function($, URI) {
'use strict';
// Accepts all jQuery.ajax() options, plus:
// corsSupport {Boolean} Set to true if the URL is known to support CORS for this domain.
// jsonpSupport {Boolean} Set to true if the URL is known to support JSONP.
$.jsonp = function(opts) {
var windowUrl = $.jsonp.getLocation(),
apiUri = $.jsonp.getApiUri(opts),
defaultDataType;
if ($.jsonp.isCrossDomain(URI(windowUrl), apiUri)) {
var doProxy;
// favor CORS because it can provide error messages from server to callbacks
if ($.support.cors) {
// use the proxy if the endpoint doesn't support CORS, or if it would be an insecure request from a secure page
if (!opts.corsSupport || $.jsonp.isInsecureRequest(apiUri)) {
// proxy CORS
doProxy = true;
} // else direct CORS
defaultDataType = 'json';
} else {
if (!opts.jsonpSupport) {
// proxy JSONP
doProxy = true;
} // else direct JSONP
defaultDataType = 'jsonp';
opts.timeout = opts.timeout || 10000; // ensures error callbacks are fired
}
if (doProxy) {
opts.data = {
url: apiUri.toString()
};
// TODO consider checking for all non-JSON types
if (opts.dataType === 'text') {
// jQuery(?) doesn't accept JSONP responses with strings passed, so non-JSON responses are wrapped with {data: "..."}.
// Mask this to the library user by simply returning the underlying string.
opts.dataFilter = function(json) {
return json.data;
};
}
opts.url = $.jsonp.PROXY;
opts.dataType = defaultDataType;
}
} else {
defaultDataType = 'json';
}
opts.dataType = opts.dataType || defaultDataType;
return $.ajax(opts);
};
$.extend($.jsonp, {
PROXY: 'https://jsonp.afeld.me/',
// make this available for easier testing
getLocation: function() {
return window.location;
},
getApiUri: function(ajaxOpts) {
var windowUrl = $.jsonp.getLocation(),
uri = URI(ajaxOpts.url).absoluteTo(windowUrl.href),
params;
if (typeof ajaxOpts.data === 'string') {
params = URI.parseQuery(ajaxOpts.data);
} else {
params = ajaxOpts.data || {};
}
uri.addSearch(params);
return uri;
},
// http://stackoverflow.com/a/1084027/358804
isCrossDomain: function(uri1, uri2) {
return (
uri1.protocol() !== uri2.protocol() ||
uri1.host() !== uri2.host() ||
uri1.port() !== uri2.port()
);
},
isInsecureRequest: function(uri) {
var windowUrl = this.getLocation();
return (
windowUrl.protocol === 'https:' && uri.protocol() !== windowUrl.protocol
);
}
});
});