简体   繁体   中英

Call web service from js file(angularjs)

i am creating a web app in MVC5 in which i need to call a web service from my javascript file,

in asp.net i used to call like

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

but in MVC5 it is showing me error

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

i tried this also

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

        }
    })

but i am facing the same error

you can use below angularjs code,

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

for more help Consuming Rest API Calls in AngularJS using $http Service

You can use the following code:-

$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.
})

Here response.data will contain the data from the server . This response object has various attributes ,You can check out more on this link :- https://docs.angularjs.org/api/ng/service/ $http

Since you're trying to communicate with an old school web service I highly recommend wrapping it on the server side by adding it as a service reference. By doing so you will be able to expose it as a JSON service which most of the client side applications today including your AngularJS application would prefer to communicate with. For more information: https://msdn.microsoft.com/en-us/library/bb628649.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM