LYiub
7 months ago
116
5
Laravel

Laravel and AJax on localhost working, but online Ajax request not working with no reponse

Posted 7 months ago by LYiub

I'm using Laravel 5.4 , my website working well on localhost, but when I upload it on live, the problem start to shown in Ajax requests and response. All Ajax requests not working in the whole website. It is clear that I just download a theme from Themeforest and start updating it, but on local everything looks working well, but the problem at online which is the URL() or the Ajax request not working anytime.

Herein an example of the code have request :

 $(document).ready(function () {

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });


        $('.allleads').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url:"{{ route('lead.GetAllData')}}",
                // url: 'lead/GetAllLeads',
                data: function (d) {
                    d.Course_Filters = 'ALL',
                        d.Status_Filters = 'ALL',
                        d.Type_leads = 'ALL',
                        d.start_page = 'first_load'
                }

            },
            columns: [
                {"data": "lead", "name": "lead"},
                {"data": "email", "name": "e
      
            ],
        // I also tried to put the following header but seems still not working too 
            // headers: {  
            //     "accept": "application/json",
            //     "Access-Control-Allow-Headers": "x-requested-with",
            //     'Access-Control-Allow-Credentials': ' true'
            // },
            bSort: false,
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        });

and for sure, the DataTable works well on local, no no problem in the server side, and now the issue when I upload this system online, the error shown when I click on any button or function have Ajax request. the is the error I have:

leads:1 Access to XMLHttpRequest at 'http://mywebsite-domain/lead/GetAllLeads?draw=1&columns%5B0%5D%5Bdata%5D=lead&columns%5B0%5D%5Bname%5D=lead&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=email&columns%5B1%5D%5Bname%5D=email&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=false&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=primary_number&columns%5B2%5D%5Bname%5D=primary_number&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=false&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=show&columns%5B3%5D%5Bname%5D=show&columns%5B3%5D%5Bsearchable%5D=false&columns%5B3%5D%5Borderable%5D=false&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=lead_type&columns%5B4%5D%5Bname%5D=lead_type&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=false&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=stage&columns%5B5%5D%5Bname%5D=stage&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=false&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B6%5D%5Bdata%5D=source_id&columns%5B6%5D%5Bname%5D=source_id&columns%5B6%5D%5Bsearchable%5D=true&columns%5B6%5D%5Borderable%5D=false&columns%5B6%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B6%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B7%5D%5Bdata%5D=status&columns%5B7%5D%5Bname%5D=status&columns%5B7%5D%5Bsearchable%5D=false&columns%5B7%5D%5Borderable%5D=false&columns%5B7%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B7%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B8%5D%5Bdata%5D=sms&columns%5B8%5D%5Bname%5D=sms&columns%5B8%5D%5Bsearchable%5D=false&columns%5B8%5D%5Borderable%5D=false&columns%5B8%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B8%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B9%5D%5Bdata%5D=assign&columns%5B9%5D%5Bname%5D=assign&columns%5B9%5D%5Bsearchable%5D=false&columns%5B9%5D%5Borderable%5D=false&columns%5B9%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B9%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B10%5D%5Bdata%5D=note&columns%5B10%5D%5Bname%5D=note&columns%5B10%5D%5Bsearchable%5D=false&columns%5B10%5D%5Borderable%5D=false&columns%5B10%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B10%5D%5Bsearch%5D%5Bregex%5D=false&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&Course_Filters=ALL&Status_Filters=ALL&Type_leads=ALL&start_page=first_load&_=1550807104368' from origin 'https://mywebsite-domain' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.



I already tried to add header to my Ajax request as following :

headers: {  
               "accept": "application/json",
               "Access-Control-Allow-Headers": "x-requested-with",
               'Access-Control-Allow-Credentials': ' true'
            },

but still not worked. I tried also to change the 'Appserviceprovider', but still can't solve the issue, here is what I change in the 'AppServiceProvider':


public function boot()
{
 if (app()->environment('remote')) {
            URL::forceScheme('https');
        }

}


public function register()
    {
        $this->app['request']->server->set('HTTPS', true); // This is the line I added but still same
        if ($this->app->environment('local', 'testing')) {
            $this->app->register(DuskServiceProvider::class);
            $this->app->register(\L5Swagger\L5SwaggerServiceProvider::class);
        }
    }

After change AppserviceProvider, I got the same error. This is the line I added : " $this->app['request']->server->set('HTTPS', true);"

I also tried to check the config/app.php:

   'url' => env('APP_URL','https://domain-name'),

So everything is right I think, and localhost working well. Why on online server not working and keep showing this error:

leads:1 Access to XMLHttpRequest at 'http://mywebsite-domain/lead/GetAllLeads?draw=1&columns%5B0%5D%5Bdata%5D=lead&columns%5B0%5D%5Bname%5D=lead&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=false&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=email&columns%5B1%5D%5Bname%5D=email&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=false&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=primary_number&columns%5B2%5D%5Bname%5D=primary_number&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=false&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=show&columns%5B3%5D%5Bname%5D=show&columns%5B3%5D%5Bsearchable%5D=false&columns%5B3%5D%5Borderable%5D=false&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=lead_type&columns%5B4%5D%5Bname%5D=lead_type&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=false&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=stage&columns%5B5%5D%5Bname%5D=stage&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=false&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B6%5D%5Bdata%5D=source_id&columns%5B6%5D%5Bname%5D=source_id&columns%5B6%5D%5Bsearchable%5D=true&columns%5B6%5D%5Borderable%5D=false&columns%5B6%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B6%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B7%5D%5Bdata%5D=status&columns%5B7%5D%5Bname%5D=status&columns%5B7%5D%5Bsearchable%5D=false&columns%5B7%5D%5Borderable%5D=false&columns%5B7%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B7%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B8%5D%5Bdata%5D=sms&columns%5B8%5D%5Bname%5D=sms&columns%5B8%5D%5Bsearchable%5D=false&columns%5B8%5D%5Borderable%5D=false&columns%5B8%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B8%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B9%5D%5Bdata%5D=assign&columns%5B9%5D%5Bname%5D=assign&columns%5B9%5D%5Bsearchable%5D=false&columns%5B9%5D%5Borderable%5D=false&columns%5B9%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B9%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B10%5D%5Bdata%5D=note&columns%5B10%5D%5Bname%5D=note&columns%5B10%5D%5Bsearchable%5D=false&columns%5B10%5D%5Borderable%5D=false&columns%5B10%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B10%5D%5Bsearch%5D%5Bregex%5D=false&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&Course_Filters=ALL&Status_Filters=ALL&Type_leads=ALL&start_page=first_load&_=1550807104368' from origin 'https://mywebsite-domain' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Please sign in or create an account to participate in this conversation.