I'm having issues with a website that I'm working on, here is what I use in localhost and server.
Localhost:Xampp, PHP 7.3
Server:VestaCP, PHP 7.4.3
Currently on every ajax post
这是JS代码。
function confirmMaintenanceOn() {
Swal.fire({
title: '',
text: 'You are about to activate maintenance mode!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes!',
showLoaderOnConfirm: true,
preConfirm() {
return new Promise(((resolve) => {
$.ajax({
url: '{{ route("maintenance_on") }}',
type: 'POST',
data: { _token: '{{csrf_token()}}' },
dataType: 'json',
})
.done((response) => {
Swal.fire('', 'Maintanance mode has been activated!', 'success');
})
.fail((response) => {
Swal.fire('', "I don't feel so good...", 'error');
console.log(response);
});
}));
},
allowOutsideClick: false,
});
}
我在控制台得到的回应是:
abort: ƒ (a)
always: ƒ ()
catch: ƒ (a)
done: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ (a)
overrideMimeType: ƒ (a)
pipe: ƒ ()
progress: ƒ ()
promise: ƒ (a)
readyState: 0
responseJSON: undefined
setRequestHeader: ƒ (a,b)
state: ƒ ()
status: 0
statusCode: ƒ (a)
statusText: "error"
then: ƒ (b,d,e)
__proto__: Object
而PHP代码只是一个简单的Artisan调用来启用维护模式。现在棘手的部分,我检查了所有的路由,我甚至把它切换到GET,它的工作,如果在我的浏览器上输入URL,我得到一个真实的响应,代码工作,但有一些与ajax.就像我在标题中说的,在本地主机上,它的工作完美无瑕,即使在调试打开,我仍然得到相同的响应,undefined。所以,如果你以前遇到过这个问题,请告诉我怎么做才能解决这个问题。
我解决了这个问题,很简单。我的问题是cloudflare将所有http链接重定向到https。从我读到的资料来看,Ajax有一个安全功能,如果链接被重定向,就无法工作。
如何解决。把所有的Ajax URL改成https:/而不是http:/,因为我用的是Laravel blade route(''),所以我不得不在里面加了两个简单的代码。app/Providers/AppServiceProvider.php
添加 use Illuminate\Support\Facades\URL;
在顶部添加 URL::forceScheme('https');
在boot()函数中。希望我帮到你,祝你好运!