PUT : 405 - Method not allowed with Spring MVC

PUT : 405 - Method not allowed with Spring MVC

PIGAYEPIGAYE Posts: 11Questions: 2Answers: 0

Hi,

Every time I try to make update like this :

                    edit: {
                                 type: 'PUT',
                                 contentType: 'application/json',
                                url:  'personnephysique/editRow?id=_id_',
                                data: function (d) {
                                     var newdata;
                                     $.each(d.data, function (key, value) {
                                          newdata = JSON.stringify(value);
                                     });
                                     return newdata;
                               }
                        }

I have this error :

"NetworkError: 405 Méthode Non Autorisée - http://localhost:8081/lasieec/personnephysique/editRow?id=29"

However I have this method in my Controller :

   /**/
    @RequestMapping(value = serviceId + "/editRow", method = RequestMethod.PUT)
    public @ResponseBody PersonnePhysiqueEntite putPersonnePhysiqueData( PersonnePhysiqueEntite personnetoPut ) {       
        personnetoPut = service.savePersonnePhysiqueEntite(personnetoPut);
        return personnetoPut;
    }
    /**/

I really nead you help.

Thank you in advance

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    I would start by looking at the server logs to see why its responding with 405 Method Not Allowed.

    Look at this:
    https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

    Maybe the server is not allowing put. Try changing to post.

    Kevin

  • PIGAYEPIGAYE Posts: 11Questions: 2Answers: 0

    PUT or POST, the result is same.

     POST http://localhost:8081/asec/personnephysique/editRow?id=29 405 Méthode Non Autorisée
    

    I don't why but with firebug I have this response :

    Allow    
    GET
    Cache-Control    
    no-cache, no-store, max-age=0, must-revalidate
    Content-Language    
    fr
    Content-Length    
    1058
    Content-Type    
    text/html;charset=ISO-8859-1
    Date    
    Wed, 26 Jul 2017 12:19:45 GMT
    Expires    
    0
    Pragma    
    no-cache
    Server    
    Apache-Coyote/1.1
    
  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    Looks like you need to enable POST and or PUT on your Apache web server. According to the above only GET is supported.

    Kevin

  • PIGAYEPIGAYE Posts: 11Questions: 2Answers: 0

    Thanks you Kevin for your response.

    The problem is not caused by Apach server because I use in the same Controller POST and PUT in many other places with simple Html button.
    But it is about using DataTables.Editor itself.

    I wonder if there is a concret exemple of using Editor with Spring MVC?

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    If I understand correctly firebug is a web browser tool for troubleshooting. I would still recommend looking at the server log to see why it is responding with 405.

    Can you post your full Datatables and Editor config?

    What does newdata contain?

    I'm not familiar with Spring MVC nor examples. Maybe someone else can help.

    Kevin

  • PIGAYEPIGAYE Posts: 11Questions: 2Answers: 0

    Hello Kevin, thank you very much for your great concern.
    As you request, I copy the entire code.
    We are going to continue digging maybe we will find a solution.
    For my part I will inform you as soon as I find a solution.

    Thank you for everything.

    Here is the code:

    <script type="text/javascript">
            /*<![CDATA[*/
    
                var editor;
                $(document).ready(function() {
                    var serviceId = document.getElementById("serviceId").value;
                    var urlAjax = serviceId + "/datatable";
    
                    editor = new $.fn.dataTable.Editor( {
                        ajax: {
                            create: {
                                type: 'POST',
                                url:  'personnephysique/create'
                            },
                            edit: {
                                     type: 'PUT',
                                     contentType: 'application/json;  charset=utf-8',
                                    url:  'personnephysique/editRow?id=_id_',
                                    "dataType": 'json',
                                    data: function (d) {
                                         var newdata;
                                         $.each(d.data, function (key, value) {
                                              newdata = JSON.stringify(value);
                                         });
                                         return newdata;
                                   }
                            },
                            remove: {
                                type: 'DELETE',
                                url:  'personnephysique/removeRow?id=_id_'
                            }
                        },
                        table: "#personne_physique_datatable",
                        idSrc:  'id',
                        fields: [
                                     {
                                           label: "Civilité:",
                                           name: "civilite.code",
                                           type:  "select",
                                        /*options: [
                                            { label: "Mme", value: "Madame" },
                                            { label: "M",   value: "Monsieur" }
                                        ]*/
                                        "ipOpts": getListCivilites()
    
                                       },{
                                           label: "Prénoms:",
                                           name: "prenoms"
                                       }, {
                                           label: "Nom:",
                                           name: "nom"
                                       }, {
                                        label: "Date de naissance:",
                                        name : 'dateNaissance'
                                    } , {
                                        label: "Profession:",
                                        name : 'profession'
                                    } , {
                                        label: "Email:",
                                        name : 'email'
                                    }
                       ]
                    } );
    
                   var table= $('#personne_physique_datatable').DataTable( {
                        dom: "Bfrtip",
                        ajax: {
                            url: urlAjax
                           ,type: "GET" //default = POST
                        },
                        serverSide: true, //Obligatoire! Sinon Erreur 400 Mauvaise requete
                        columns: [
                            { data: "civilite.code" },
                            { data: null, render: function ( data, type, row ) {
                                return data.civilite.code + ' ' + data.prenoms+' '+ data.nom;
                            } },
                            { data: "dateNaissance" },
                            { data: "profession" },
                            { data: "email" },
                            {
                                data : 'actions',
                                orderable : false,
                                class : 'buttons',
                                searchable : false,
                                render : function(data, type, row) {
                                    if (row.id) {
                                        var functions='';
                                        return functions;
    
                                    }
                                }
                            }
                        ],
                        select: true,
                        buttons: [
                            { extend: "create", editor: editor },
                            { extend: "edit",   editor: editor},
                              { extend: "remove", editor: editor }
                        ]
                    } );
    
                    function getListCivilites(){      
                        var test= new Array({"label" : "a", "value" : "a"});
                        test.splice(0,1);
                        $.ajax({
                          "url": 'personnephysique/civilites',
                          "async": false,
                          "dataType": 'json',
                          "success": function (json) {
                              for(var a=0;a < json.length;a++){
                                obj= { "label" : json[a].code, "value" : json[a].libelle};
                                test.push(obj);
                              }
                            },
                          "error" : function( jqXHR, textStatus, errorThrown ){ console.log( jqXHR, textStatus, errorThrown ); }
                        });
                        return test;
                    }
                } );
                /*]]>*/       
        </script>
    
  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin

    This isn't a client-side issue (unless its using the wrong HTTP verb or the work URL). The problem is with the routing on the server-side or some other way it is processing the URL. It is throwing an error - perhaps in a security context, perhaps being it is an unexpected request. But either way, the issue likes in the server's configuration rather than the Javascript you are using.

    Allan

  • lsukharnlsukharn Posts: 10Questions: 3Answers: 2

    I have exactly the same issue. When I used DataTables without the Editor POST worked fine. After adding the Editor plugin I started getting "Failed to load resource: the server responded with a status of 405 (METHOD NOT ALLOWED)" for some reason. I am working with Python/Flask and use development server, which has the least number of security restrictions

  • lsukharnlsukharn Posts: 10Questions: 3Answers: 2
    Answer ✓

    I fixed the issue. There was a problem on the back-end

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin

    Thanks for posting back. GOod to hear it is working now.

    Allan

  • arabbaniarabbani Posts: 1Questions: 0Answers: 0

    @lsukharn , Can you point out how you fixed it. I'm also facing the same problem

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    You would need to look at your server's logs to determine why its responding with a 405 Method Not Allowed error.

    Kevin

This discussion has been closed.