title: 401 Unauthorized DELETE request to RESTful API in laravel via Ajax
body: <p>I've created a restful API using <strong>laravel</strong> controllers. I have a <code>PhotosController</code> which has a <code>destroy($id)</code> method for resource deletion. also I have a piece of <strong>javascript</strong> code that sends a <code>DELETE</code> request to my app. the result should be the deletion of the photo with <code>$id</code> id. but laravel doesn't route my request to <code>destroy</code> method. instead it sends an <strong>401 Unauthorized</strong> error.</p>
  
  <p>the thing is that I want to send <code>DELETE</code> request to my app via <code>Ajax</code>, but <strong>laravel</strong> doesn't let my request to be routed!</p>
  
  <p><strong>routes.php file :</strong></p>
  
  <pre><code>Route::resource('photos', 'PhotosController');
  </code></pre>
  
  <p><strong>destroy method :</strong></p>
  
  <pre><code>public function destroy($id)
  {
      try{
          unlink($_SERVER["DOCUMENT_ROOT"].'/uploads/doctors/' . $id);
          Session::forget('photo');
          $msg = Notification::where('flag', 's')-&gt;where('code', 'user-update-delete-photo-gallery')-&gt;first()-&gt;msg;
          return Response::json(array('success' =&gt; $msg));
      }catch (Exception $e){
          App::abort(500, $e-&gt;getMessage());
      }
  }
  </code></pre>
  
  <p><strong>my Ajax request :</strong></p>
  
  <pre><code>$.ajax(
      {
          url: "/photos/" + name,
          method : "DELETE", // Or POST : result is the same
          data :{
              _token : $("input[name=_token]").val(),
              _method : 'DELETE'
          },
          success: function(data, textStatus, jqXHR ){
              parent.replaceWith("");
              toastr.success(data['success']);
              $("#overlay").hide();
          },
          beforeSend : function(jqXHR, settings ){
              $("#overlay").show();
          },
          error : function(jqXHR, textStatus, errorThrown ){
              toastr.error(jqXHR.responseText);
              $("#overlay").hide();
          }
      }
  );
  </code></pre>
  
  <p>Thanks for your help.</p>