【问题标题】:Laravel ajax url not working on production serverLaravel ajax url 在生产服务器上不起作用
【发布时间】:2019-05-13 20:51:12
【问题描述】:

我在我的 Laravel 项目中有这个 ajax 请求(这是一个简单的版本,但它正在工作):

$.ajax({
            method: 'POST', // Type of response and matches what we said in the route
            url: '/admin/lessons/addMember/licenseMemberId', // This is the url we gave in the route
            data: {'licenseMemberId' : id},
            success: function(response){ 
                console.log(response);

                if ($.trim(response)) {
                    var actualMembers = document.getElementById("actual-member");
                }
                $('#membersModal').modal('hide');
            },  
        });

当我在本地工作并使用 php artisan 服务时,ajax 调用有效,但是当我部署到生产服务器时不起作用(因为路径 /admin/lessons/addMember/licenseMemberId 不是服务器中的完整路径) . 最好的方法应该是使用路线,但我不知道如何。 这是路由表:

web                                          |
|        | POST      | admin/lessons/addMember/{licenseMemberId}                     | lessons.addMember               | App\Http\Controllers\admin\LessonController@addMember  

有没有办法使用带有参数的 laravel 路由?如果没有,我该怎么办?

【问题讨论】:

  • 在 post 方法中,您可以发送带有 ajax 数据参数的 {licenseMemberId},而不是路由参数。
  • 你能发布你的控制器方法吗?

标签: javascript ajax laravel deployment routing


【解决方案1】:

你不应该像在 vanila php 或 html 中那样使用 URL,使用 URL function,这个函数确保你的路由正确指向项目根目录,在你的情况下你可以做类似的事情这个

$.ajax({
        method: 'GET', 
        url: '{{URL::to('/admin/lessons/addMember/')}}' + id, 
// Laravel will print the url and you just need to concat your id to it
        success: function(response){ 
            console.log(response);

            if ($.trim(response)) {
                var actualMembers = document.getElementById("actual-member");
            }
            $('#membersModal').modal('hide');
        },  
    });

请注意,我正在使用 GET,因为您似乎是在检索数据而不是发布它,但是如果您需要发布它,那么 MisaGH 答案就是要走的路

【讨论】:

    【解决方案2】:

    路由中不接收参数。

    网址应该是:/admin/lessons/addMember

    $.ajax({
                method: 'POST', // Type of response and matches what we said in the route
                url: '/admin/lessons/addMember', // This is the url we gave in the route
                data: {'licenseMemberId' : id},
                success: function(response){ 
                    console.log(response);
    
                    if ($.trim(response)) {
                        var actualMembers = document.getElementById("actual-member");
                    }
                    $('#membersModal').modal('hide');
                },  
            });
    

    还有控制器:

    $member_id = request('licenseMemberId');
    

    【讨论】:

    • 谢谢,但是当我将您的代码放入生产服务器时仍然无法工作,我点此控制台错误:bosco.com/admin/lessons/addMember/licenseMemberId -->未找到,问题是我的项目位于它应该位于的子目录中成为bosco.com/sg3000/public//admin/lessons/addMember/…
    • 如果 /sg3000/public 真的是你的公共网址的一部分,你需要去那里......虽然有点奇怪
    • 所以最好的做法是当我有POST方法时不要使用路由参数传递参数?或者只有当我在 ajax 脚本中使用它时才好?因为我在代码的其他部分也使用了这条路线,但是普通的php代码......
    • 你可以为所欲为。但我认为这种 ajax 调用的方法要好得多。
    【解决方案3】:

    真正的路线是

    admin/lessons/addMember/{licenseMemberId}   
    

    所以在javascript中你需要调用

    '/admin/lessons/addMember/' + id 
    

    其中 id 是一个变量。在控制器中,您可以使用 id 获取

    Input::get('licenseMemberId') or $request->get('licenseMemberId'); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 2016-12-20
      • 2017-02-18
      • 2019-01-27
      • 1970-01-01
      • 2016-05-15
      相关资源
      最近更新 更多