【问题标题】:VestaCP | Laravel Ajax doesn't work on server but works in localhost灶神星 | Laravel Ajax 不能在服务器上工作,但可以在 localhost 上工作
【发布时间】:2020-10-07 02:08:38
【问题描述】:

我正在处理的网站有问题,这是我在 localhost 和服务器中使用的。

本地主机: Xampp,PHP 7.3

服务器: 灶神星CP,PHP 7.4.3

目前在每个 ajax post/get 方法上我都没有从服务器得到任何反馈,我只是收到一个未定义的 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 上它可以完美地工作,即使打开了调试,我仍然得到相同的响应,未定义。因此,如果您之前遇到过这个问题,请告诉我我能做些什么来解决它。

【问题讨论】:

    标签: javascript laravel server vps vestacp


    【解决方案1】:

    我解决了这个问题,仙女容易。我的问题是 cloudflare 将所有 http 链接重定向到 https。根据我的阅读,Ajax 有一个安全功能,如果链接被重定向,它将无法工作。

    如何解决:只需将所有 ajax url 更改为 https:// 而不是 http://,因为我使用的是 Laravel 刀片路由(''),我必须在其中添加 2 个简单代码 app/Providers/AppServiceProvider.php 在顶部添加use Illuminate\Support\Facades\URL; 并在 boot() 函数中添加URL::forceScheme('https');。希望我能帮到你,祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多