简体   繁体   English

Laravel Ajax URL在生产服务器上不起作用

[英]Laravel ajax url not working on production server

I have this ajax request on my Laravel Project (this is a simple version but it is working): 我在Laravel Project上收到了这个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');
            },  
        });

When I work locally and I use php artisan serve the ajax call works, but when i deploy to my production server doesn't (because the path /admin/lessons/addMember/licenseMemberId is not the full path in the server). 当我在本地工作并且使用php artisan服务时,ajax调用可以工作,但是当我部署到我的生产服务器时则不行(因为路径/ admin / lessons / addMember / licenseMemberId不是服务器中的完整路径)。 The best way shoud be using the route, but I don't know how. 最好的方法应该是使用路线,但我不知道如何。 This is the routing table: 这是路由表:

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

Is there a way to use the laravel route with the paramether? 有没有办法将laravel途径与抛物线一起使用? If not, how can I do? 如果没有,我该怎么办?

You shouldn't use URLs in Laravel the way you use it in vanila php or html, use the URL function , this function ensures that your route is pointing currectly to the project root, in your case you can do something like this 您不应该像在vanila php或html中那样使用Laravel中的URL,而应使用URL函数 ,该函数可确保您的路由当前指向项目根,在这种情况下,您可以执行以下操作

$.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');
        },  
    });

Notice that im using GET as you seem to be retrieving data and not POSTING it, however if you need to post it, the MisaGH answer is the way to go 请注意,即时通讯似乎在使用GET而不是发布数据,但是如果您需要发布数据,则MisaGH答案是解决之道

Do not receive the parameter in route. 在路由中不接收参数。

URL should be : /admin/lessons/addMember 网址应为: /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');
            },  
        });

And the controller: 和控制器:

$member_id = request('licenseMemberId');

The real route is 真正的路线是

admin/lessons/addMember/{licenseMemberId}   

so in javascript you need to call 因此在javascript中,您需要调用

'/admin/lessons/addMember/' + id 

where id is a variable. 其中id是一个变量。 In the controller you can use the id getting 在控制器中,您可以使用id获取

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

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

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