简体   繁体   English

Laravel尝试重定向所有jQuery Ajax请求

[英]Laravel trying to redirect for all jquery ajax request

Whenever i try to do some ajax calls in the page, it works good in localhost but in the live its throwing error like this 每当我尝试在页面中进行一些Ajax调用时,它在localhost中都可以正常工作,但在实时运行中会像这样抛出错误

在此处输入图片说明

Though i have did this in RedirectIfAuthenticated.php 虽然我已经在RedirectIfAuthenticated.php做到了

if (Auth::guard($guard)->check()) {
     //return redirect('admin/dashboard');
}

Pls help me with this 请帮我

i hope that following code you put in JS file and elsewhere in meta of the page. 我希望您将以下代码放入JS文件以及页面的meta中的其他位置。 for Ajax calling. 用于Ajax调用。

========================================== ==========================================

In JS File write $(document).ready(function () {...} 在JS文件中写$(document).ready(function(){...}

========================================== ==========================================

      $.ajaxSetup(
        {
           headers:
            {
                'X-CSRF-Token': $('input[name="_token"]').val()
            }
        });    

It also verify AJAX call while Request and Response. 它还在请求和响应时验证AJAX调用。

this is a browser-ajax-CORS problem 这是一个浏览器ajax-CORS问题

solution1: the laravle-enable-cors-composer-package into your project 解决方案1:将laravle-enable-cors-composer-package打包到您的项目中

solution2: add following line into your ajax-controller-method 解决方案2:将以下行添加到您的ajax-controller-method中

    header("Access-Control-Allow-Origin: *");
    // ALLOW OPTIONS METHOD
    $headers = [
        'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
        'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
    ];
    if($request->getMethod() == "OPTIONS") {
        // The client-side application can set only headers allowed in Access-Control-Allow-Headers
        return Response::make('OK', 200, $headers);
    }

solution3: config the appache nginx 解决方案3: 配置设备 nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM