![](/img/trans.png)
[英]Laravel\Vue - ajax file upload not working on the production server
[英]Laravel ajax url not working on production server
我在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');
},
});
當我在本地工作並且使用php artisan服務時,ajax調用可以工作,但是當我部署到我的生產服務器時則不行(因為路徑/ admin / lessons / addMember / licenseMemberId不是服務器中的完整路徑)。 最好的方法應該是使用路線,但我不知道如何。 這是路由表:
web |
| | POST | admin/lessons/addMember/{licenseMemberId} | lessons.addMember | App\Http\Controllers\admin\LessonController@addMember
有沒有辦法將laravel途徑與拋物線一起使用? 如果沒有,我該怎么辦?
您不應該像在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');
},
});
請注意,即時通訊似乎在使用GET而不是發布數據,但是如果您需要發布數據,則MisaGH答案是解決之道
在路由中不接收參數。
網址應為: /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');
真正的路線是
admin/lessons/addMember/{licenseMemberId}
因此在javascript中,您需要調用
'/admin/lessons/addMember/' + id
其中id是一個變量。 在控制器中,您可以使用id獲取
Input::get('licenseMemberId') or $request->get('licenseMemberId');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.