Error using POST AngularJS

0

My controller is like this

app.controller("listaTelefonicaController", function ($scope, $http)

Inside of it is my add function

ctrl.adicionarContatos = function (contato) {
            contato.telefone = ctrl.formatCel(contato.telefone);
            console.log(contato);
            var data = JSON.stringify(contato);
            $http.post("/contatos.json", {cont: data},{headers: {'Content-Type': 'application/json'}}).then(
                 function (response) {
                     console.log("success:" + response);
                     delete ctrl.contato;
                     $scope.contatoForm.$setPristine();
                     return response;
                 },
                 function (response) {
                     console.log(response);
                     return response;
                 }
             );

            // $http({
            //         url: 'http://127.0.0.1:5500/contatos.json',
            //         method: "POST",
            //         headers: {
            //             'Content-Type': 'application/json',
            //             'Accept': 'application/json'
            //         },
            //         data: contato
            //     })
            //     .then(function (response) {
            //             console.log("success:" + response);
            //         },
            //         function (response) { // optional
            //             console.log("Fail:" + response.data);
            //         });
            };

I spent a few hours searching and testing in various ways and all of them appeared the same error

"POST link 405 (Method Not Allowed)"

This file is populated with some data like this below:

{
    "nome": "Larissa",
    "telefone": "9999-3399",
    "cor": "orange",
    "data": "2015-04-12T12:53:46.204Z",
    "operadora": {
        "nome": "GVT",
        "codigo": 25,
        "categoria": "Fixo"
    }
}

GET did everything right, but when I try to do POST it gives this error, I've been looking for a few hours and can not find a way to solve my problem. Enters direct POST error

Thanks for any help!

    
asked by anonymous 14.06.2018 / 20:38

1 answer

0

@EduardoBobato I'm not sure what I'm going to tell you now because I've never seen this anywhere and the answer is in my head but I think you can not give a post to a .json file, since json is not a language probably does not support post type variables.

If your server supports php try to change the file contacts.json to contacts.php and put inside the code:

<?php
print_r(
  json_encode(
    [
        "nome"=> "Larissa",
        "telefone"=> "9999-3399",
        "cor"=> "orange",
        "data"=> "2015-04-12T12:53:46.204Z",
        "operadora"=> [
            "nome"=> "GVT",
            "codigo"=> 25,
            "categoria"=> "Fixo"
        ]
    ]
  )
);
    
15.06.2018 / 03:08