增加功能:$.bc增加跨域带Cookie请求数据cors: true

This commit is contained in:
Argo-MacBookPro 2018-09-10 01:19:26 +08:00
parent a0e827ce82
commit eff349fe61
2 changed files with 13 additions and 7 deletions

View File

@ -123,6 +123,7 @@
loadingTimeout: 10000, loadingTimeout: 10000,
callback: false, callback: false,
$element: false, $element: false,
cors: false,
contentType: 'application/json', contentType: 'application/json',
dataType: 'json', dataType: 'json',
method: 'get' method: 'get'
@ -143,19 +144,25 @@
if (options.url) { if (options.url) {
var data = options.method === 'get' ? options.data : JSON.stringify(options.data); var data = options.method === 'get' ? options.data : JSON.stringify(options.data);
$.ajax({ var ajaxSettings = {
url: $.formatUrl(options.url) + options.id, url: $.formatUrl(options.url) + options.id,
data: data, data: data,
method: options.method, method: options.method,
contentType: options.contentType, contentType: options.contentType,
dataType: options.dataType, dataType: options.dataType,
crossDomain: false,
success: function (result) { success: function (result) {
success(result); success(result);
}, },
error: function (XMLHttpRequest, textStatus, errorThrown) { error: function (XMLHttpRequest, textStatus, errorThrown) {
success(false); success(false);
} }
};
if (options.cors) $.extend(ajaxSettings, {
xhrFields: { withCredentials: true },
crossDomain: true
}); });
$.ajax(ajaxSettings);
} }
function success(result) { function success(result) {
if ($.isFunction(options.callback)) { if ($.isFunction(options.callback)) {

View File

@ -57,14 +57,14 @@
else options.url = item.Url; else options.url = item.Url;
$.bc({ $.bc({
url: options.url, url: options.url,
method: "post", cors: !item.Self,
callback: function (result) { callback: function (result) {
if ($.isArray(result)) { if ($.isArray(result)) {
var html = '<div class="cache-item"><i class="fa fa-ellipsis-v"></i><div><span data-toggle="tooltip" title="{2}">{2}</span><span class="badge badge-pill badge-success">{0}</span></div><span title="{3}">{3}</span><div><span>{7}</span><button class="btn btn-danger" title="{1}" data-url="{5}?cacheKey={1}" data-toggle="tooltip" data-self="{6}" data-placement="left"><i class="fa fa-trash-o"></i></button></div></div>'; var html = '<div class="cache-item"><i class="fa fa-ellipsis-v"></i><div><span data-toggle="tooltip" title="{2}">{2}</span><span class="badge badge-pill badge-success">{0}</span></div><span title="{3}">{3}</span><div><span>{6}</span><button class="btn btn-danger" title="{1}" data-url="{4}?cacheKey={1}" data-toggle="tooltip" data-self="{5}" data-placement="left"><i class="fa fa-trash-o"></i></button></div></div>';
var content = result.sort(function (x, y) { var content = result.sort(function (x, y) {
return x.Key > y.Key ? 1 : -1; return x.Key > y.Key ? 1 : -1;
}).map(function (ele) { }).map(function (ele) {
return $.format(html, ele.Interval, ele.Key, ele.Desc, ele.Value, '', $.format(item.Url, ele.Key), item.Self, Math.max(0, ele.Interval - Math.round((new Date() - new Date(ele.CreateTime.replace(/-/g, '/'))) / 1000))); return $.format(html, ele.Interval, ele.Key, ele.Desc, ele.Value, $.format(item.Url, ele.Key), item.Self, Math.max(0, ele.Interval - Math.round((new Date() - new Date(ele.CreateTime.replace(/-/g, '/'))) / 1000)));
}).join(''); }).join('');
$sortable.append($.format('<h6 class="cache-title">{0}</h6>', item.Desc)); $sortable.append($.format('<h6 class="cache-title">{0}</h6>', item.Desc));
$sortable.append(content); $sortable.append(content);
@ -80,7 +80,6 @@
}); });
}; };
var listCache = function (options) { $.bc({ url: options.url, method: "post" }); };
$('a[data-method]').on('click', function (e) { $('a[data-method]').on('click', function (e) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -96,8 +95,8 @@
listCacheUrl(options); listCacheUrl(options);
}).last().trigger('click'); }).last().trigger('click');
$sortable.on('click', '.btn', function () { $sortable.on('click', '.btn', function () {
$(this).tooltip('dispose'); var $this =$(this).tooltip('dispose');
listCache({ url: $(this).attr('data-url') }); $.bc({ url: $this.attr('data-url'), cors: $this.attr('data-self') === 'false' });
listCacheUrl(); listCacheUrl();
}); });