2016-07-20 17 views
2

laravel 5.0とajaxを使用しているときにajax 404エラーが発生しましたが、理由がわかりません。Laravel ajax 404が見つかりませんでした。

マイルート

Route::post('user/saved/ [ 
'as' => 'delete-topics', 
'uses' => '[email protected]',  
]); 

マイフォーム

<form action="{{action('[email protected]')}}" method="post" class="deleteform"> 
     <input type="hidden" name="topic_id" value="{{$topic->topic_id}}"> 

     <input type="submit" value="delete"> 
     </form> 

私のAJAX呼び出し

// process the form 
$('.deleteform').submit(function(event) { 
event.preventDefault(); 
    var $form = $(this); 


    var formData = { 
    topic_id : $form.find('input[name=topic_id]').val() 
}; 


// process the form 
$.ajax({ 
    type : 'POST', 
    url  : 'saved', 
    data : formData, 
    dataType : 'json', 
    encode : true 
}) 

私のコントローラ

public function deleteTopic() 
{ 

    $topic_id = Request::get('topic_id'); 

    if(Auth::check()) 
    { 
     $user_id = Auth::user()->id; 
    $delete_topic = DB::table('topic_save')->where('user_id', $user_id)->where('topic_id', $topic_id)->delete(); 
    } 


    $data['success'] = true; 
    $data['message'] = 'success'; 
    echo json_encode($data); 
} 

私はルートを照合するために「ユーザー/保存」にAjaxの呼び出しでURLを変更しようとしているが、それでも同じ404エラーに

みんなありがとうを取得しています。

+0

フォームを送信しているページのURLは何ですか? – Jonathon

答えて

3

フォームに設定したアクションにURLを変更します。それは次のようになります。

url = $form.attr('action'); 
$.ajax({ 
    type : 'POST', 
    url  : url, 
    data : formData, 
    dataType: 'json', 
    encode : true 
}) 
0

あなたのフォームアクション自体は間違っています:

代わりにこの使用の
<form action="{{action('[email protected]')}}" method="post" class="deleteform"> 

:あなたは自分のフォームからアクションプロパティを削除することができます

action="route_path" 
2

、あなたのためにドンフォーム送信を使用しない場合は、次のようにajaxを変更してください:

$.ajax({ 
    type : 'POST', 
    url  : "{{ route('delete-topics') }}", 
    data : formData, 
    dataType: 'json', 
    encode : true 
}) 
関連する問題