簡體   English   中英

Laravel Ajax 404未找到錯誤

[英]Laravel ajax 404 not found error

使用laravel 5.0和ajax時出現ajax 404錯誤,我無法弄清原因。

我的路線

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

我的表格

    <form action="{{action('TopicController@deleteTopic')}}" 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更改為“ user / saved”以匹配路由,但仍然遇到相同的404錯誤

多謝你們。

將url更改為您為表單設置的操作。 那將是:

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

您可以只從表單中刪除操作屬性,因為您不使用表單提交,然后像這樣更改ajax:

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

您的表單操作本身是錯誤的:

<form action="{{action('TopicController@deleteTopic')}}" method="post" class="deleteform">

代替這種用法:

action="route_path"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM