2016-01-15 4 views
6

Ho un percorso con questo percorso405 Metodo non ammessi in Symfony 3

/** 
* @Method({"DELETE"}) 
* @Route("/secure/users") 
*/ 

Quando provo a fare un ricciolo

<html> 
 
    <head> 
 
     <meta charset="UTF-8" /> 
 
     <title>An Error Occurred: Method Not Allowed</title> 
 
    </head> 
 
    <body> 
 
     <h1>Oops! An Error Occurred</h1> 
 
     <h2>The server returned a "405 Method Not Allowed".</h2> 
 

 
     <div> 
 
      Something is broken. Please let us know what you were doing when this error occurred. 
 
      We will fix it as soon as possible. Sorry for any inconvenience caused. 
 
     </div> 
 
    </body> 
 
</html>

ho cercato di abilitare anche

Request::enableHttpMethodParameterOverride(); 

in app.dev e app_dev.php, infatti posso gestire le richieste PUT.

+0

Come si esegue la richiesta? Il metodo HTTP è effettivamente DELETE (dal messaggio di errore sembra che tu abbia inviato una richiesta con un diverso metodo HTTP)? – xabbuh

risposta

1

Aggiungere questo parametro per la vostra richiesta ricciolo:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 

o in linea di comando

curl -X DELETE "http://localhost/secure/users" 

Se si sta eseguendo una richiesta XHR utilizzando jQuery, basta fare

$.ajax({ 
    url: '/secure/users', 
    type: 'DELETE', 
    data: { id: resourceToDelete } 
    success: function(result) { 
     // Do something with the result 
    } 
}); 

E per puro javascript:

var req = new XMLHttpRequest(); 
req.open('DELETE', '/secure/users'); 
req.setRequestHeader("Content-type", "application/json"); 
req.send({ id: 'entityIdentifier' }); 

Se si desidera accedere dal browser o passare parametri di query come /secure/users?id=x, utilizzare GET:

/** 
* @Method({"GET"}) 
* @Route("/secure/users") 
*/ 

Vedi What is the usefulness of PUT and DELETE HTTP request methods?

+0

Ciao, I miei parametri vengono da Query Parameter, devo abilitare DELETE, perché sto usando CURL solo per test, ma in realtà questa chiamata proviene da javascript – monkeyUser

+0

MAI utilizzare un GET per eliminare oggetti sul server! Altrimenti potresti un giorno chiedervi perché a volte gli oggetti scompaiano casualmente (causati dai prefetch dei browser, ad esempio, per esempio). Se non vuoi usare Javascript, usa un modulo con POST. – aferber