Add CORS support with JSONP fallback. Closes #2260

This commit is contained in:
Antonin Delpeuch 2019-12-24 10:38:12 +01:00
parent ad261109be
commit 2e1d5402ea
5 changed files with 52 additions and 14 deletions

View File

@ -1258,7 +1258,7 @@
xhr.getResponseHeader("X-Metaweb-TID")); xhr.getResponseHeader("X-Metaweb-TID"));
} }
}, },
dataType: "jsonp", dataType: o.access === undefined ? "jsonp" : o.access,
cache: true cache: true
}; };
@ -1508,7 +1508,7 @@
xhr.getResponseHeader("X-Metaweb-TID")); xhr.getResponseHeader("X-Metaweb-TID"));
} }
}, },
dataType: "jsonp", dataType: o.access === undefined ? "jsonp" : o.access,
cache: true cache: true
}; };
if (o.flyout_lang) { if (o.flyout_lang) {

View File

@ -68,15 +68,12 @@ ReconciliationManager.registerStandardService = function(url, f, silent) {
dismissBusy = DialogSystem.showBusy($.i18n('core-recon/contact-service')+"..."); dismissBusy = DialogSystem.showBusy($.i18n('core-recon/contact-service')+"...");
} }
$.ajax( var registerService = function(data, mode) {
url,
{ "dataType" : "jsonp",
"timeout":10000
}
)
.success(function(data, textStatus, jqXHR) {
data.url = url; data.url = url;
data.ui = { "handler" : "ReconStandardServicePanel" }; data.ui = {
"handler" : "ReconStandardServicePanel",
"access" : mode
};
index = ReconciliationManager.customServices.length + index = ReconciliationManager.customServices.length +
ReconciliationManager.standardServices.length; ReconciliationManager.standardServices.length;
@ -91,6 +88,28 @@ ReconciliationManager.registerStandardService = function(url, f, silent) {
if (f) { if (f) {
f(index); f(index);
} }
};
// First, try with CORS (default "json" dataType)
$.ajax(
url,
{ "dataType" : "json",
"timeout":5000
}
)
.success(function(data, textStatus, jqXHR) {
registerService(data, "json");
})
.error(function(jqXHR, textStatus, errorThrown) {
// If it fails, try with JSONP
$.ajax(
url,
{ "dataType" : "jsonp",
"timeout": 5000
}
)
.success(function(data, textStatus, jqXHR) {
registerService(data, "jsonp");
}) })
.error(function(jqXHR, textStatus, errorThrown) { .error(function(jqXHR, textStatus, errorThrown) {
if (!silent) { if (!silent) {
@ -98,6 +117,7 @@ ReconciliationManager.registerStandardService = function(url, f, silent) {
alert($.i18n('core-recon/error-contact')+': ' + textStatus + ' : ' + errorThrown + ' - ' + url); alert($.i18n('core-recon/error-contact')+': ' + textStatus + ' : ' + errorThrown + ' - ' + url);
} }
}); });
});
}; };
ReconciliationManager.unregisterService = function(service, f) { ReconciliationManager.unregisterService = function(service, f) {

View File

@ -222,6 +222,10 @@ ReconStandardServicePanel.prototype._wireEvents = function() {
var suggestOptions = $.extend({}, this._service.suggest.type); var suggestOptions = $.extend({}, this._service.suggest.type);
suggestOptions.key = null; suggestOptions.key = null;
suggestOptions.query_param_name = "prefix"; suggestOptions.query_param_name = "prefix";
// CORS/JSONP support
if (this._service.ui && this._service.ui.access) {
suggestOptions.access = this._service.ui.access;
}
input.suggestT(suggestOptions); input.suggestT(suggestOptions);
} }
@ -246,6 +250,10 @@ ReconStandardServicePanel.prototype._rewirePropertySuggests = function(type) {
var suggestOptions = $.extend({}, this._service.suggest.property); var suggestOptions = $.extend({}, this._service.suggest.property);
suggestOptions.key = null; suggestOptions.key = null;
suggestOptions.query_param_name = "prefix"; suggestOptions.query_param_name = "prefix";
// CORS/JSONP support
if (this._service.ui && this._service.ui.access) {
suggestOptions.access = this._service.ui.access;
}
if (type) { if (type) {
suggestOptions.type = typeof type == "string" ? type : type.id; suggestOptions.type = typeof type == "string" ? type : type.id;
} }

View File

@ -212,6 +212,10 @@ DataTableCellUI.prototype._render = function() {
if ('view' in service && 'url' in service.view && !('view_url' in suggestOptions)) { if ('view' in service && 'url' in service.view && !('view_url' in suggestOptions)) {
suggestOptions.view_url = service.view.url; suggestOptions.view_url = service.view.url;
} }
// CORS / JSONP support
if (service.ui && service.ui.access) {
suggestOptions.access = service.ui.access;
}
addSuggest = true; addSuggest = true;
} }

View File

@ -85,6 +85,12 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
if (service && service.suggest && service.suggest.entity) { if (service && service.suggest && service.suggest.entity) {
suggestOptions = $.extend({}, service.suggest.entity); suggestOptions = $.extend({}, service.suggest.entity);
suggestOptions.query_param_name = "prefix"; suggestOptions.query_param_name = "prefix";
// CORS / JSONP support
if (service.ui && service.ui.access) {
suggestOptions.access = service.ui.access;
}
if ('view' in service && 'url' in service.view && !('view_url' in suggestOptions)) { if ('view' in service && 'url' in service.view && !('view_url' in suggestOptions)) {
suggestOptions.formatter_url = service.view.url; suggestOptions.formatter_url = service.view.url;
} }