Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 14700

Angular and Web API

$
0
0
Boas!

Eu quero fazer um pedido a uma Web API, mas não estou a conseguir.
O pedido chega a Web API mas não retorna os dados.
Se eu escrever o endereço diretamente no browser retorna dados, se for pelo javascript dá erro.

Código (C):
public class ClientController : ApiController
        {

                // GET: api/Client
                public IEnumerable<string> Get()
                {
                        return new string[] {"Teste1","Teste2"};
                }
....


Código (Javascript):
(function () {
        var app = angular.module("mainApp", []).controller("ClientController", function ($scope, $http) {
                $scope.Clients = ["Ricardo","Maria"];
         
                $http({
                        method: 'GET',
                        url: 'http://localhost:60525/api/Client/'
                 
                }).then(function successCallback(response) {
                        // this callback will be called asynchronously
                        // when the response is available
                        $scope.Clients = response.data;
                }, function errorCallback(response) {
                        // called asynchronously if an error occurs
                        // or server returns response with an error status.
                        alert("Erro: " + response);
                });
                $scope.Title = "Titulo";
        });
})();

Viewing all articles
Browse latest Browse all 14700