从js文件调用Web服务(angularjs)

时间:2016-11-05 05:29:57

标签: javascript asp.net angularjs web-services

我正在MVC5中创建一个Web应用程序,我需要从我的javascript文件中调用一个Web服务,

在asp.net中的

我以前称之为

$http.get('/welcome.asmx/welcomegrid', {
        params: {
        }
    })

但在MVC5中它显示错误

Request format is unrecognized for URL unexpectedly ending in '/welcomegrid'

我也尝试了这个

$http.get("http://localhost:50353/Models/welcome.asmx/welcomegrid", {
        params:
        {

        }
    })

但我面临同样的错误

3 个答案:

答案 0 :(得分:0)

你可以使用下面的angularjs代码,

$scope.getData = function() {
        $scope.loading = true;
        $http.get("http://jsonplaceholder.typicode.com/posts/1")
        .then(function(response){ 
            $scope.details = response.data;
            $scope.loading = false;
        });
    }   

获取更多帮助Consuming Rest API Calls in AngularJS using $http Service

答案 1 :(得分:0)

您可以使用以下代码: -

$http.get("http://localhost:50353/Models/welcome.asmx/welcomegrid")
.then(function(response){
    //This function will be executed when the request was completed successfully
}, function(response){
    //This function will be executed when the request was not successful.
})

此处response.data将包含来自服务器的数据。 此响应对象具有各种属性,您可以在此链接上查看更多: - https://docs.angularjs.org/api/ng/service/ $ HTTP

答案 2 :(得分:0)

由于您尝试与旧的学校Web服务进行通信,我强烈建议您将其作为服务参考添加到服务器端。通过这样做,您将能够将其公开为JSON服务,当今大多数客户端应用程序(包括您的AngularJS应用程序)都希望与之通信。有关详细信息:https://msdn.microsoft.com/en-us/library/bb628649.aspx