[英]VestaCP | Laravel Ajax doesn't work on server but works in localhost
我正在处理的网站有问题,这是我在本地主机和服务器中使用的。
本地主机:Xampp,PHP 7.3
服务器:VestaCP,PHP 7.4.3
目前在每个 ajax 发布/获取方法上,我没有从服务器获得任何反馈,我只是收到未定义的 json 回复。
这是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 有问题。 就像我在标题中所说的那样,在 localhost 上它可以完美运行,即使打开了调试,我仍然得到相同的响应,未定义。 所以,如果你以前遇到过这个问题,请告诉我我能做些什么来解决它。
我解决了这个问题,仙女容易。 我的问题是 cloudflare 将所有 http 链接重定向到 https。 根据我的阅读,Ajax 具有安全功能,如果链接被重定向,它将无法工作。
How to fix: Simply change all ajax urls to https:// instead of http://, as I'm using Laravel blade route(''), I had to add 2 simple codes in app/Providers/AppServiceProvider.php
Add use Illuminate\Support\Facades\URL;
在顶部并添加URL::forceScheme('https');
在引导() function。 希望我对你有帮助,祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.