繁体   English   中英

Laravel中的AJAX调用返回Http异常

AJAX call in Laravel returns Http Exception

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

当我尝试在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');

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

2 个回复

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

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

<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

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

1 如何从 Laravel 5 中的 AJAX 调用返回视图?

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

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

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

6 AJAX在Angular中调用HTTP

我有一个现有的应用程序,我转移到Angular,我有一些问题。 我的新Angular应用程序有四条路线: 家 活动 登录 注册 在我的旧应用程序中,我对PHP进行了AJAX调用,它将查询MySQL并根据需要返回信息,但我可以使用jQuery将所有内容绑 ...

7 重复AJAX调用并返回http代码

当单击一个按钮时,我有一个jQuery AJAX调用,例如: 来自服务器的所有数据都将打印到div: 服务器上的blah.php进行了一些处理以检索一些状态代码: 响应代码( $r-&gt;getResponseCode() )按预期返回,所以我的问题是我用setTime ...

8 返回HTTP 302的Ajax调用

当我们进行此Ajax调用时,间歇性地得到HTTP状态代码为302。在大多数情况下,Ajax调用成功并且返回HTTP状态为200 OK。 我无法获得确切原因以及如何克服这种情况。 当我将应用程序空闲10-15分钟然后进行此调用时,在特定情况下会返回HTTP状态302。 但是,当我直接在浏览器 ...

10 在AJAX调用中返回AJAX

我想返回第二个ajaxcall作为ajax函数的结果,任何人都可以帮助我。 csrfWithoutDone函数: 顺便说一下:这是用打字稿写的,但是如果用函数替换private并删除:(类型)它也适用于js。 ...

暂无
暂无

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

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