How to call a C# service using Angularjs

Asked

Viewed 128 times

0

Good afternoon, so I’m doubting how to call a service in C#, I’ve tried several ways but failed.

(function () {
    'use strict';

    function Service($http, $q) {
        const url = 'http://172.23.1.31:81/Service1.svc';

        const servico = {
            obterTodos: obterTodos,
            excluir: excluir,
            obter: obter,
            criar: criar,
            alterar: alterar
        };

        //Obter nomes banco de dados

        function obterTodos() {
            const promise = $http.get(url);
            return promise.then(aoRequisicaoConcluir, aoRequisicaoFalhar);
        }

        //obter um apelido específico:

        function obter(id) {
            const urlComId = url + '/' + id;
            const promise = $http.get(urlComId);
            return promise.then(aoRequisicaoConcluir, aoRequisicaoFalhar);
        }

        //Excluir apelido

        function excluir(id) {
            const urlComId = url + '/' + id;
            const promise = $http.delete(urlComId);
            return promise.then(aoRequisicaoConcluir, aoRequisicaoFalhar);
        }

        //Criar apelido novo

        function criar(TesteSafra) {
            const promise = $http.post(url, TesteSafra);
            return promise.then(aoRequisicaoConcluir, aoRequisicaoFalhar);
        }

        //Alterar um apelido:

        function alterar(TesteSafra) {
            const urlComId = url + '/' + TesteSafra.id
            const promise = $http.put(urlComId, TesteSafra);
            return promise.then(aoRequisicaoConcluir, aoRequisicaoFalhar);
        }

        //promise

        function aoRequisicaoConcluir(response) {
            return response.data;
        }

        function aoRequisicaoFalhar(response) {
            return $q.reject(response.statusText);
        }

        return servico;
    }

    safraService.$inject = ['$http', '$q'];

    angular.module('app')
        .factory('Service', Service);
})();
  • Is this url correct? What is the answer to the request? In Chrome: F12 -> Network and see the result.

1 answer

0

Probably the injection of $http and $q, the name of the service being injected is different from the name of the one used. Being the Function as Service and the injection into safraService.

(function () {
'use strict';

-->   function Service($http, $q) {
        ...
       }

-->   safraService.$inject = ['$http', '$q'];

angular.module('app')
    .factory('Service', Service); <--
})();

Browser other questions tagged

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