我的問題是不行的AJAX Laravel對我來說,在ver5.2如何解決這個問題 我的錯誤:未定義AJAX Laravel 5.2留言
Route [category] not defined. (View: C:\wamp\www\pc\resources\views\admin\category.blade.php)
我的路線文件:
Route::group(['middleware' => 'web'], function() {
Route::auth();
Route::get('category', '[email protected]');
});
Route::group(['middleware' => 'web'], function() {
Route::auth();
Route::post('category', '[email protected]');
});
我控制器:
public function create(messageRequest $request)
{
try {
Category::create($request->all());
return response()->json(array('sms'=>'save Success'));
}catch (Exception $e){
return response()->json(array('err'=>'error'));
}
}
我的javascript:
<script>
$('#submit').on('click', function (e) {
e.preventDefault();
var data = $('#create').serialize();
$.ajax({
type: 'post',
url: '{!! URL::route('category') !!}',
data: data,
success: function (data) {
alert(data.sms);
console.log('data');
},
error:function(){
alert(data.err);
console.log('data');
}
});
});
</script>
你應該ê xplain *爲什麼*這是一個解決方案:'URL :: route('category')'尋找**命名的**路由,並且OP沒有命名它們的路由。 'URL :: to('category')'也適用於非命名路由。 – ceejayoz
true,URL :: to將是一個更容易的修復,但命名路線通常看起來像一個好主意 – peterstarling
完全同意。我只是想詳細說明不同之處。 – ceejayoz