简体   繁体   English

从AngularJs调用Restful api函数

[英]Calling Restful api function from AngularJs

I have Restful API service in asp.net, which provides code for JavaScript to use, i need to use its functions in angular. 我在asp.net中有Restful API服务,它提供了JavaScript使用的代码,我需要在angular中使用它的函数。 I am posting code here which has RestAPI function and it includes calls to data and it should return JSON data to calling function i believe. 我在这里发布了具有RestAPI功能的代码,它包含对数据的调用,它应该将JSON数据返回给我相信的调用函数。 please if anybody familiar with the solution your response will be highly appreciated. 如果有任何人熟悉该解决方案,我们将非常感谢您的回复。 Thank-you. 谢谢。

function RestAPI(){ self = this; }
RestAPI.prototype = {
    self: null,
    urlString: "http://exclusiveautosales.azurewebsites.net/ExclusiveAutoSales_Handler.ashx",
    GetFiltersMain:function(successFunction,failFunction,token) {
        var data = { 'interface': 'RestAPI', 'method': 'GetFiltersMain', 'parameters': {}, 'token': token };
        var jsonData = dojo.toJson(data);
        var xhrArgs = {
            url: self.urlString,
            handleAs: 'json',
            postData: jsonData,
            load: successFunction,
            error: failFunction };
        var deferred = dojo.xhrPost(xhrArgs);
    },
    GetMakeModelPrice:function(make_id,model_id,min_price,max_price,successFunction,failFunction,token) {
        var data = { 'interface': 'RestAPI', 'method': 'GetMakeModelPrice', 'parameters': {'make_id':make_id,'model_id':model_id,'min_price':min_price,'max_price':max_price}, 'token': token };

        var jsonData = dojo.toJson(data);
        var xhrArgs = {
            url: self.urlString,
            handleAs: 'json',
            postData: jsonData,
            load: successFunction,
            error: failFunction };
        var deferred = dojo.xhrPost(xhrArgs);
    },
    GetSearchResult:function(search_q,make_id,model_id,year_id,engine_id,color_id,body_id,transmission_id,fueltype_id,subfeature_id,minPrice,maxPrice,successFunction,failFunction,token) {
        var data = { 'interface': 'RestAPI', 'method': 'GetSearchResult', 'parameters': {'search_q':search_q,'make_id':make_id,'model_id':model_id,'year_id':year_id,'engine_id':engine_id,'color_id':color_id,'body_id':body_id,'transmission_id':transmission_id,'fueltype_id':fueltype_id,'subfeature_id':subfeature_id,'minPrice':minPrice,'maxPrice':maxPrice}, 'token': token };

        var jsonData = dojo.toJson(data);
        var xhrArgs = {
            url: self.urlString,
            handleAs: 'json',
            postData: jsonData,
            load: successFunction,
            error: failFunction };
        var deferred = dojo.xhrPost(xhrArgs);
    }
};

This may help you to solve ASP.NET Web Service In AngularJS Using $HTTP 这可以帮助您使用$ HTTP在AngularJS中解决ASP.NET Web服务

user $http service for Restful 用户$http服务Restful

var app = angular.module('myApp', []);
       app .controller('RestAPI',function($scope, $http) {                    
                $http({
                    method : "GET",
                    url : "http://exclusiveautosales.azurewebsites.net/ExclusiveAutoSales_Handler.ashx",
                    data :{ 'interface': 'RestAPI', 'method': 'GetFiltersMain', 'parameters': {}, 'token': token }
                }).then(function mySucces(response) {
                    $scope.jsonData = response.data;
                }, function myError(response) {
                    $scope.jsonData = response;
                });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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