当我尝试在laravel中使用ajax时,显示以下错误,

{“ message”:“”,“ exception”:“ Symfony \\ Component \\ HttpKernel \\ Exception \\ HttpException”,“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Exceptions \\ Handler.php“,” line“:203,” trace“:[{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Exceptions \\ Handler.php “,” line“:175,” function“:” prepareException“,” class“:” Illuminate \\ Foundation \\ Exceptions \\ Handler“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ app \\ Exceptions \\ Handler.php“,” line“:49,” function“:” render“,” class“:” Illuminate \\ Foundation \\ Exceptions \\ Handler“,” type“:”->“ },{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php”,“ line”:83,“ function”:“ render”,“ class “:”“ App \\ Exceptions \\ Handler”,“类型”:“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php “,” line“:55,” function“:” handleException“,” class“:” Illuminate \\ Rout ing \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ View \\ Middleware \\ ShareErrorsFromSession.php“,” line“:49,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:151,” function“:” handle“,” class“:” Illuminate \\ View \\ Middleware \\ ShareErrorsFromSession“ ,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php”,“ line”:53,“ function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Session \\ Middleware \\ StartSession.php“,” line“:63,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“ ,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ larave l \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:151,” function“:” handle“,” class“:” Illuminate \\ Session \\ Middleware \\ StartSession“,” type“:”-> “},{”文件“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“,” line“:53,” function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Cookie \\ Middleware \\ AddQueuedCookiesToResponse.php”,“行”:37,“功能”:“ Illuminate \\ Routing \\ {closure}”,“ class”:“ Illuminate \\ Routing \\ Pipeline”,“ type”:“-> “},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:151,” function“:” handle“,” class“:” Illuminate \\ Cookie \\ Middleware \\ AddQueuedCookiesToResponse“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php”,“行”:53,“功能”:“ Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Cookie \\ Middleware \\ EncryptCookies.php”,“行”:59,“功能”:“ Illuminate \\ Routing \\ {closure}”,“类”:“ Illuminate \\ Routing \\ Pipeline”,“类型”:“-> “},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:151,” function“:” handle“,” class“:” Illuminate \\ Cookie \\ Middleware \\ EncryptCookies“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“,” line“:53,” function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{”文件“:”“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php”,“ line”:104,“ function”:“ Illuminate \\ Routing \\ {closure}”,“ class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Router.php”,“行”:667,“功能”:“然后”,“类”:“ Illuminate \\ Pipeline \\ Pipeline”,“类型”:“-> “},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Router.php“,” line“:642,” function“:” runRouteWithinStack“,” class“:” Illuminate \\ Routing \\ Router“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Router。 php”,“行”:608,“功能”:“ runRoute”,“类”:“ Illuminate \\ Routing \\ Router”,“类型”:“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Router.php“,” line“:597,” function“:” dispatchToRoute“,” class“:” Illuminate \\ Routing \\ Router“,” type“ :“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Kernel.php”,“行”:176,“功能” :“调度”,“类别”:“ Illuminate \\ Routing \\ Router”,“类型”:“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“,” line“:30,” function“:” Illuminate \\ Foundation \\ Http \\ {closure}“,” class“:” Illuminate \\ Foundation \\ Http \\内核”,“类型”:“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ fideloper \\ proxy \\ src \\ TrustProxies.php”,“行”:57,“功能” :“ Illuminate \\ Routing \\ {closure}”,“ class”:“ Illuminate \\ Routing \\ Pipeline”,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php”,“行”:151,“功能”:“句柄”,“类”:“ Fideloper \\ Proxy \\ TrustProxies”,“类型”:“->”} ,{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php”,“ line”:53,“ function”:“ Illuminate \\ Pipeline \\ {closure }“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Middleware \\ TransformsRequest.php“,” line“:31,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline。 php“,” line“:151,” function“:” handle“,” class“:” Illuminate \\ Foundation \\ Http \\ Middleware \\ TransformsRequest“,” type“:”->“},{” file“:” C :\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“,” line“:53,” function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Middleware \\ TransformsRequest。 php“,” line“:31,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{” file“: “ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php”,“ line”:151,“ function”:“ handle”,“ class”:“ Illuminate \\ Foundation \\ Http \\ Middleware \\ TransformsRequest“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“,” line“:53,” function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“ ,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Middleware \\ ValidatePostSize.php”,“行” :27,“ function”:“ Illuminate \\ Routing \\ {closure}”,“ class”:“ Illuminate \\ Routing \\ Pipeline”,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:151,” function“:”句柄“,” class“:” Illuminate \\ Foundation \\ Http \\ Middleware \\ ValidatePostSize“ ,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php”,“ line”:53,“ function“:” Illuminate \\ Pipeline \\ {closure}“,” class“:” Illuminate \\ Pipeline \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\供应商\\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Middleware \\ CheckFo rMaintenanceMode.php“,” line“:62,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{”文件“:”“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php”,“ line”:151,“ function”:“ handle”,“ class”:“ Illuminate \\ Foundation \\ Http \\ Middleware \\ CheckForMaintenanceMode“,”类型“:”->“},{”文件“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Routing \\ Pipeline.php“ ,“行”:53,“功能”:“ Illuminate \\ Pipeline \\ {closure}”,“ class”:“ Illuminate \\ Pipeline \\ Pipeline”,“ type”:“->”},{“ file”:“ C :\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Pipeline \\ Pipeline.php“,” line“:104,” function“:” Illuminate \\ Routing \\ {closure}“,” class“:” Illuminate \\ Routing \\ Pipeline“,” type“:”->“},{” file“:” C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Kernel.php“ ,“行”:151,“功能”:“然后”,“类”:“ Illuminate \\ Pipeline \\ Pipeline”,“类型”: “->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Foundation \\ Http \\ Kernel.php”,“行”:116,“功能”: “ sendRequestThroughRouter”,“类”:“ Illuminate \\ Foundation \\ Http \\ Kernel”,“ type”:“->”},{“ file”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ public \\ index.php”, “行”:55,“函数”:“句柄”,“类”:“ Illuminate \\ Foundation \\ Http \\ Kernel”,“类型”:“->”},{“文件”:“ C:\\ xampp \\ htdocs \\ TravelAssistant \\ server.php“,” line“:21,” function“:” require_once“}]}

我的控制器代码是

public function getPublicAttraction(Request $request)
    {
        return response(['msg'=>'success']);
    }

阿贾克斯电话,

<script>
    function getAttraction()
    {

        var pass= {'_token':$('meta[name="csrf-token"]').attr('content')

    };
    $.ajax({
            type:'POST',
            url:'{{route("Attractions")}}',
            datatype:'json',
            data: pass,
            success:function(data){
                $("#public_attractions").html(data.msg);
            }
        }).fail(function(jqXHR, textStatus, error){
            $("#public_attractions").html(jqXHR.responseText);
        });


    }
    $(document).ready(function(){
         getAttraction();
    });
</script>

带有csrf令牌的元标记,

<meta name="csrf-token" content="!!{csrf_token()}!!">

路由文件web.php条目,

Route::post('/getAttractions','AjaxController@getPublicAttraction')->name('Attractions');

我想我被困住了,请帮忙。

#1楼 票数:0

为什么要重新发明行之有效的东西。

刀片中的第一个令牌应如下所示:(无论出于何种原因,您都有额外的!!)

<meta name="csrf-token" content="{{ csrf_token() }}">

2nd将此代码放在主刀片中的某个位置:

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

来源https://laravel.com/docs/5.7/csrf#csrf-x-csrf-token

#2楼 票数:0

好的,除了CSRF元标记的方式外,其他一切看起来都不错,因为您不需要转义csrf令牌中的任何内容,因此打印令牌的正确方法将是: <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}"> Token- <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}">这是我无法给您的最佳猜测,因为您没有提供响应的HTTP状态是什么,也不提供抛出异常的哪种情况,异常消息也为空所以..

  ask by Raymond Thomas translate from so

未解决问题?本站智能推荐:

1回复

Http异常Laravel5.6

我的http异常有问题。 实际上,我创建的404视图不会出现。 仅显示默认视图(当我在URL http://127.0.0.1:8000/fr/sdfsdf编写时)。 视图默认为: 我的视图显示图像,标题和错误原因以及视图路径: resources/views/errors/404
4回复

如何在Laravel8中注册自定义异常处理程序

在 Laravel 7 中,这段代码工作正常。 使用renderable方法也适用于 laravel 8。但我不确定如何在创建CustomException类后在 laravel 8 中注册它。
2回复

如何解决Laravel中的“MethodNotAllowedHttpException”错误?

我正在Laravel中开发护照API。 我收到“ MethodNotAllowedHttpException”。 我不知道该怎么办。
1回复

将参数传递给laravel中的控制器函数时出错

我试图将变量$ id传递给路由文件中的Controller函数。 以下是各个文件中的片段: Routes.php BlogController.php 我在RouteCollection.php第161行中收到NotFoundHttpException: 我尝试搜索原因,但找
1回复

Laravel,如何在App\\Exceptions\\Handler.php中的handle()中获取控制器实例或类名

我环顾四周,似乎找不到关于此的答案或讨论。 是否可以在App \\ Exceptions \\ Handler.php的“ handle()”方法中获取原始控制器的实例? 我想知道如何执行此操作,以便知道发生异常的控制器。 谢谢!
12回复

如何从Laravel5中的AJAX调用返回视图?

我正在尝试获取一个 html 表以在 ajax 调用时返回。 路线: 调用页面上的ajax: 控制器: 看法: 我在 json.html 数据中没有得到任何东西。 没有。 如果在控制器中我说: 这工作得很好。 如何从 Laravel 5 中的 ajax 调用返回视图。
1回复

如何在Laravel中强制返回对AJAX请求的HTTP响应

我使用jQuery快速使用Laravel模板创建单页体验。 例如,当点击管理面板侧边栏上的按钮(链接)时,仅更新网页的#content部分。 我通常会在请求中添加success和error回调 在Laravel的早期版本(例如5.4中,错误函数内部的JS通常像正常的HTTP响应(正常的
2回复

无法通过Ajax调用返回laravel下载

我希望用户下载文件,它实际上并不需要通过Ajax进行下载,但这是我发现的最简单的方法,但是它不起作用。 这是我正在调用的Controller中的方法: 这是我的ajax函数: 在此追加内部调用此函数: 这是我的路线,它位于一个名为panel的组内: 我的第一个问题是:是否可以