Giter Club home page Giter Club logo

batch.js's Introduction

Batch.js

Batch.js is a tiny JavaScript library that enables sending batch requests to ASP.NET Web API using jQuery.

What is request batching

Batching is a way to pack several different API requests into the single HTTP POST request. The ASP.NET Web API middleware then unpacks the request and re-routs the individual requests to the appropriate API methods. Similarly, multiple responses are then going to be packed together and sent back to the client as a single HTTP response. This feature allows you to decrease the traffic between the server and the client dramatically making the entire communication much less "chatty" (and therefore, making your API more scalable).

Below are some resources that were useful with batching support in ASP.NET Web API.

http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx http://aspnetwebstack.codeplex.com/wikipage?title=Web+API+Request+Batching

Using the code

The batching support is implemented via ajaxBatch method which can be called on a global jQuery object. The semantics of ajaxBatch method are more or less the same as with the native ajax method.

Below is an example of performing a batch request to a handler available at /api/batch:

$.ajaxBatch({
url: '/api/batch',
    data: [
        {
            type: 'GET',
            url: '/api/posts'
        },
        {
            type: 'POST',
            url: '/api/posts',
            data: {
                Title: 'Batch support in Web API'
            }
        }
    ],
    complete: function (xhr, status, data) {
        alert(data.length) // 2
    }
});

As you can see, there's an additional argument "data" which is passed to "complete" handler. The argument contains an array object, where each element of the array corresponds to a single API response within the batch (the library assumes that the order of the responses within the batch is the same as the order of the requests, although this can be customized at the server level). Each element represents an object with two fields:

  • status - HTTP status code (e.g. 200).
  • data - Evaluated response data (if any).

Dependencies

Batch.js depends on jQuery >= 1.5.x. The library also takes advantage of the native JSON library (e.g. parse and stringify methods). If browsers are being targeted that don't support JSON object natively, refer to: https://github.com/douglascrockford/JSON-js.

Known issues

Currently Batch.js only supports application/json as content type (and therefore, enforces it when sending the request and receiving the response).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.