✨ ajax替换为axios
This commit is contained in:
2308
public/js/app.js
vendored
2308
public/js/app.js
vendored
File diff suppressed because it is too large
Load Diff
13
resources/js/app.js
vendored
13
resources/js/app.js
vendored
@@ -44,9 +44,9 @@ window.utils = {
|
||||
$btn.text(loadingText).addClass('disabled')
|
||||
},
|
||||
success(response) {
|
||||
options.success && options.success.call(props, response);
|
||||
options.success && options.success.call(props, response.data);
|
||||
},
|
||||
complete(xhr, status) {
|
||||
complete(data) {
|
||||
props.loading = false;
|
||||
if (props.finished) {
|
||||
// no more
|
||||
@@ -57,9 +57,10 @@ window.utils = {
|
||||
if (opts.data.page !== undefined) {
|
||||
opts.data.page++;
|
||||
}
|
||||
options.complete && options.complete.call(props, xhr, status)
|
||||
options.complete && options.complete.call(props, data)
|
||||
},
|
||||
error() {
|
||||
error(error) {
|
||||
// response = error.response
|
||||
$btn.text(errorText).addClass('disabled')
|
||||
setTimeout(() => $btn.text(errorText).removeClass('disabled'), 3000)
|
||||
}
|
||||
@@ -75,7 +76,9 @@ window.utils = {
|
||||
if (params) {
|
||||
opts.data = $.extend(opts.data, params)
|
||||
}
|
||||
$.ajax(opts);
|
||||
|
||||
opts.beforeSend();
|
||||
axios.get(opts.url, {params: opts.data}).then(opts.success).catch(opts.error).finally(opts.complete);
|
||||
};
|
||||
|
||||
// 首次加载
|
||||
|
||||
37
resources/js/bootstrap.js
vendored
37
resources/js/bootstrap.js
vendored
@@ -11,30 +11,33 @@ toastr.options = {
|
||||
"preventDuplicates": false,
|
||||
}
|
||||
|
||||
window.$.ajaxSetup({
|
||||
dataType: 'json',
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
|
||||
},
|
||||
statusCode: {
|
||||
401: () => {
|
||||
toastr.warning('状态失效,请先登录账号');
|
||||
},
|
||||
500: () => {
|
||||
toastr.warning('服务出现异常,请稍后再试');
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* We'll load the axios HTTP library which allows us to easily issue requests
|
||||
* to our Laravel back-end. This library automatically handles sending the
|
||||
* CSRF token as a header based on the value of the "XSRF" token cookie.
|
||||
*/
|
||||
|
||||
// window.axios = require('axios');
|
||||
window.axios = require('axios');
|
||||
|
||||
// window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
|
||||
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
|
||||
|
||||
axios.interceptors.request.use(function (config) {
|
||||
return config;
|
||||
}, function (error) {
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
axios.interceptors.response.use(function (response) {
|
||||
return response;
|
||||
}, function (error) {
|
||||
if (401 === error.response.status) {
|
||||
toastr.warning('状态失效,请先登录账号');
|
||||
}
|
||||
if (500 === error.response.status) {
|
||||
toastr.warning('服务出现异常,请稍后再试');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
/**
|
||||
* Echo exposes an expressive API for subscribing to channels and listening
|
||||
|
||||
@@ -285,18 +285,13 @@
|
||||
// confirm create
|
||||
$albums.off('click', CREATE_ID + ' a').on('click', CREATE_ID + ' a', function (e) {
|
||||
let $form = $(this).siblings('form');
|
||||
$.ajax({
|
||||
url: $form.attr('action'),
|
||||
type: 'post',
|
||||
data: $form.serialize(),
|
||||
success: response => {
|
||||
let $errorMessage = $albums.find(CREATE_ID + ' .error-message').html('').hide();
|
||||
if (response.status) {
|
||||
$form.get(0).reset();
|
||||
resetAlbums()
|
||||
} else {
|
||||
$errorMessage.html('<i class="fas fa-exclamation-circle"></i> ' + response.message).show();
|
||||
}
|
||||
axios.post($form.attr('action'), $form.serialize()).then(response => {
|
||||
let $errorMessage = $albums.find(CREATE_ID + ' .error-message').html('').hide();
|
||||
if (response.status) {
|
||||
$form.get(0).reset();
|
||||
resetAlbums()
|
||||
} else {
|
||||
$errorMessage.html('<i class="fas fa-exclamation-circle"></i> ' + response.message).show();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user