B
B
BonBon Slick2016-10-28 18:14:46
JavaScript
BonBon Slick, 2016-10-28 18:14:46

Where is the bug in Ajax request (Laravel 5.3)?

All HTML

<body>
<table>
      <tbody>
             <tr>
                  <td>
                      User id: {{ $user->id }}
                   </td>
                   <td>
           <button  class="btn btn-danger btn-block delete_single"
         data-id="{{ $user->id }}" data-token="{{ csrf_token() }}">
              Delete
           </button>
                     </td>
               </tr>
        </tbody>
</table>
</body>

All AJAX :
<script>
             jQuery(document).ready(function($) {

              $(".delete_single").click(function(){
                var id = $(this).data("id");
                var token = $(this).data("token");

                $.ajax(
                {
                  url: "/users/delete/id="+id,
                  type: 'DELETE',
                   data: {
                    "id": id,
                    "_method": 'DELETE',
                    "_token": token,
                  },
                  done: function (){
                    console.log("SUCCESSS!!!");
                  },
                  fail: function(){
                    console.log("ERROR!");
                  } 
                });
              });

            });
          </script>

Controller
public function delete( Request $user_id ){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}

route
Route::delete('users/delete/id={user_id}', '[email protected]');

Errors:
// в консоли
DELETE http://lara53/users/delete/id=14     500 (Internal Server Error)
// на странице по ссылке удаляемого ID
MethodNotAllowedHttpException in RouteCollection.php line 218:
...
at RouteCollection->methodNotAllowed(array('DELETE')) in RouteCollection.php line 205


My .htaccess
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<Limit GET POST PUT DELETE>
  Allow from all
</Limit>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2016-10-28
@Denormalization

public function delete( Request $user_id ){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}

Here you need to remove Request. And the route is better to just do:
And finally read about PSR .

B
bx-coder, 2016-10-28
@bx-coder

I don't know much about Lara, but I think that's the way it should be.

public function delete(Request $request, $user_id){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}

and if $request is not used then at all
public function delete($user_id){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}

Or am I missing something)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question