asp.net - 如何使用 Angular 调用 Asp.Net Web API 2.0 http 批处理请求?

标签 asp.net api angularjs asp.net-web-api

我在支持 Http 批处理的 Asp.Net Web Api 2.0 中创建了一个示例 Web API。 http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx描述了这个例子。

我如何使用 Angular 调用这个批处理 API?

最佳答案

我知道这是一个迟到的答案,但我已经创建了一个 Angular 模块来启用 HTTP 批处理请求。模块在这里 https://github.com/jonsamwell/angular-http-batcher我在这里写了一篇关于它的博客文章 http://jonsamwell.com/batching-http-requests-in-angular/ .

该模块对开发人员是透明的,因此一旦您包含它并设置批处理端点,对该服务的所有调用都将自动进行批处理。如果您在开始使用它时需要任何帮助,请告诉我。

例如下面的 3 个 http 请求将被自动批处理并变成一个请求!

var app = angular.module('httpBatchExample', ['jcs.angular-http-batch']);

app.config([  
    'httpBatchConfigProvider',
    function (httpBatchConfigProvider) {
        // setup some configuration to tell the module that requests to 
        // 'https://api.myapp.com' can be batched and send the batch request to https://api.myapp.com/batch
        httpBatchConfigProvider.setAllowedBatchEndpoint(
            'https://api.myapp.com',
            'https://api.myapp.com/batch');
    }
]);

app.controller('mainCtrl', [  
    '$scope',
    '$http',
    function ($scope, $http) {
        // Get the data as normal which will automatically be turned into a single HTTP request
        $http.get('https://api.myapp.com/products').then(function (data) {
            console.log('success 0 - ' + data.data);
        }, function (err) {
            console.log('error 0 - ' + err);
        });

        $http.get('https://api.myapp.com/products/2').then(function (data) {
            console.log('success 1 - ' + data.data);
        }, function (err) {
            console.log('error 1 - ' + err);
        });

        $http.put('https://api.myapp.com/products', {
            Name: 'Product X',
            StockQuantity: 300
        }).then(function (data) {
            console.log('success 2 - ' + data.data);
        }, function (err) {
            console.log('error 2 - ' + angular.fromJson(err));
        });
    }]);

关于asp.net - 如何使用 Angular 调用 Asp.Net Web API 2.0 http 批处理请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002410/

相关文章:

c# - Asp.net 计时器和更新面板

python - 从目标 Maya Python API 查找 Blendshape

javascript - 如何在 angularjs Ionic 中将对象转换为数组

javascript - Angular.js : How to order objects with orderby

c# - 从字典中获取第一个元素

asp.net - 从下拉列表中获取选定的值

jquery - 来源http ://localhost is not allowed by Access-Control-Allow-Origin.?

javascript - 是否可以在 Angular 2 中动态更改全局样式表?

asp.net - ASP.Net 中的 WF 和并行性需要指导

javascript - React-Native — 全局变量不变