2016-08-12 79 views
2

我想POST請求發送路由形式的script.jsAjax請求未通過路由在laravel 5.2

這是我的script.js

$("input[type=checkbox]").on("click",function() { 
alert('hello') 
$.ajax({ 
    type: "POST", 
    url :"load_task", 
    data: 1, 
}); 

});

這是我的路線

Route::post('load_task',function(){ 
    return "funk you post"; 
}); 
Route::get('load_task',function(){ 
    return "funk you get"; 
}); 

我只需要更新數據庫中的複選框的狀態, 什麼,我試圖做的是發送從控制器點擊複選框,當Ajax請求並更新數據庫

但我在調用路由時遇到問題。提前致謝。

回答

1

ajax中的url只是一個字符串。所以這只是'load_task'。但要打上路線,你需要像http://www.example.com/load_task這樣的東西。因此,你需要使用{{url('load_task'}}生成完整的URL,然後將它傳遞給AJAX

SUGGESTION

插入一個新的隱藏輸入說id="url"並設置生成的URL作爲其值。

<input type="hiddel" id="url" value="{{url('load_task'}}"> 

,並傳遞AJAX

$("input[type=checkbox]").on("click",function() { 
    alert('hello'); 
    $.ajax({ 
      type: "POST", 
      url : $("#url").val(), 
      data: 1, 
     }); 
});