Error using POST Angularjs

Asked

Viewed 57 times

0

My controller is like this:

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

Inside it is my adding 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 appeared the same mistake:

POST http://127.0.0.1:5500/contacts.json 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"
    }
}

The GET it all worked out, but when I try to do with the POST He gives this mistake, I’ve spent a few hours searching and I can’t find a way to solve my problem. Directly enters the POST error option.

  • Last time I checked, Angular didn’t need var data = JSON.stringify(contato);. Angular by default already does this (and with header application/json)

  • I did it to try to solve the problem, but even without it does not work

1 answer

0

@Eduardobobato I’m not sure what I’m going to tell you now because I’ve never seen it anywhere and the answer is in my head but I don’t think Voce can give a post for a file. json, since json is not a programming language it probably does not have support for post type variables.

If your server supports php try to change the.json contacts file to.php contacts 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"
        ]
    ]
  )
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.