jquery ajax xhr2,GitHub - acigna/jquery-ajax-native: jQuery Plugin that adds XHR2 responseType suppo...

Description

XHR2 provides a response attribute, that contains a converted response to a native object, depending on the responseType ("arraybuffer", "blob", "document", "json").

See:

Using this mechanism provides better performances compared to a javascript way of conversion. For example, converting an ArrayBuffer, leads to a heavy loop (See: http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String).

This jQuery Plugin adds XHR2 responseType support, that is currently lacking support in jQuery.ajax and probably remains

in the future (See the last comments on this pull request).

Please, take a look at XHR2 current support here for more precision on the support status.

Usage

The plugin provides two means to get a native ajax response as follows.

Using jQuery.ajax

The Plugin provides the native data type for the ajax method that you should use to have a native response. You should also precise the responseType on xhrFields to precise the expected native data type. This usage method allows more customization but

it is more verbose.

$.ajax({

dataType: 'native',

url: dataUrl,

xhrFields: {

responseType: 'arraybuffer'

},

success: successCallback

});

Using jQuery.getNative wrapper method

The getNative method is a shortened version of the previous method. It allows to specify a success callback only, but returns a promise, and so permets to add the error case.

//With success callback

$.getNative( dataUrl, successCallback );

//Using the returned promise (success and error callbacks respectively)

$.getNative( dataUrl ).then( successCallback, errorCallback );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值