0

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/get method I'm not getting any feedback from the server, I just receive an undefined json back.

Here's the JS code:

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,
  });
}

The response I get in the console is:

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

And the PHP code is just a simple Artisan call to enable maintenance mode. Now the tricky part, I checked all the routed, I even switched it to GET and it worked if enter the URL on my browser, I get a true response, code works but there's something off with ajax. Like I said in the title, on localhost it works flawlessly, even with debug turned on, I still get the same response, undefined. So please if you had this problem before, let me know what I can do to fix it.

1 Answers1

0

I solved the issue, fairy easy. My problem was cloudflare redirecting all http links to https. From what I've read, Ajax has a security feature where if a link is redirected it won't work.

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; at top And add URL::forceScheme('https'); in boot() function. Hopefully I helped you, good luck!