如何避免重复定义单个 Ajax 错误函数
How to avoid repetition defining a single Ajax Error Function
我正在为我的项目使用 laravel。在我的项目中存在许多 ajax 请求。对于错误处理,我正在使用此代码分区。
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrol ediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
},
我会针对每个请求重复一遍。
我想写成这样error: '{{ ajaxError() }}'
作为辅助方法。但我无法处理它。 blade 中有没有办法做到这一点?我不想重复自己。
可能从 blade 输出回调名称不是最好的解决方案
如果您使用 JQuery,您可以将全局错误处理程序附加到每个页面:(将其放在页面脚本的开头)
$(document).ajaxError(function (event, jqXHR, settings, exception)
{
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrolediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
} );
这样你甚至不必为每个请求指定一个错误回调,但每次 ajax 调用都会 return 一个错误,全局回调将被执行
如果您不想使用 jquery,您可以定义一个回调函数:
function errorCallback (event, jqXHR, settings, exception)
{
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrolediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
}
并将其附加到每个 ajax 请求:
error: errorCallback
我正在为我的项目使用 laravel。在我的项目中存在许多 ajax 请求。对于错误处理,我正在使用此代码分区。
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrol ediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
},
我会针对每个请求重复一遍。
我想写成这样error: '{{ ajaxError() }}'
作为辅助方法。但我无法处理它。 blade 中有没有办法做到这一点?我不想重复自己。
可能从 blade 输出回调名称不是最好的解决方案
如果您使用 JQuery,您可以将全局错误处理程序附加到每个页面:(将其放在页面脚本的开头)
$(document).ajaxError(function (event, jqXHR, settings, exception)
{
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrolediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
} );
这样你甚至不必为每个请求指定一个错误回调,但每次 ajax 调用都会 return 一个错误,全局回调将被执行
如果您不想使用 jquery,您可以定义一个回调函数:
function errorCallback (event, jqXHR, settings, exception)
{
if (jqXHR.status === 0) {
alert('Bağlantı sağlanamadı.\n Lütfen internet bağlantınızı kontrolediniz.');
} else if (jqXHR.status === 404) {
alert('Sayfa bulunamadı. [404]');
} else if (jqXHR.status === 401) {
window.location.replace('{{ route('login') }}');
} else if (jqXHR.status === 500) {
alert('Sunucu Hatası [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.'+ jqXHR.responseText);
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax istemi durduruldu.');
} else {
alert('Beklenmeyen Hata.\n' + jqXHR.responseText);
}
}
并将其附加到每个 ajax 请求:
error: errorCallback