Reqwest 使用教程
来源:互联网 发布:矩阵的幂次方 编辑:程序博客网 时间:2024/05/21 10:28
API
reqwest('path/to/html', function (resp) { qwery('#content').html(resp)})reqwest({ url: 'path/to/html' , method: 'post' , data: { foo: 'bar', baz: 100 } , success: function (resp) { qwery('#content').html(resp) }})reqwest({ url: 'path/to/html' , method: 'get' , data: [ { name: 'foo', value: 'bar' }, { name: 'baz', value: 100 } ] , success: function (resp) { qwery('#content').html(resp) }})reqwest({ url: 'path/to/json' , type: 'json' , method: 'post' , error: function (err) { } , success: function (resp) { qwery('#content').html(resp.content) }})reqwest({ url: 'path/to/json' , type: 'json' , method: 'post' , contentType: 'application/json' , headers: { 'X-My-Custom-Header': 'SomethingImportant' } , error: function (err) { } , success: function (resp) { qwery('#content').html(resp.content) }})// Uses XMLHttpRequest2 credentialled requests (cookies, HTTP basic auth) if supportedreqwest({ url: 'path/to/json' , type: 'json' , method: 'post' , contentType: 'application/json' , crossOrigin: true , withCredentials: true , error: function (err) { } , success: function (resp) { qwery('#content').html(resp.content) }})reqwest({ url: 'path/to/data.jsonp?callback=?' , type: 'jsonp' , success: function (resp) { qwery('#content').html(resp.content) }})reqwest({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , jsonpCallbackName: 'bar' , success: function (resp) { qwery('#content').html(resp.content) }})reqwest({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , success: function (resp) { qwery('#content').html(resp.content) } , complete: function (resp) { qwery('#hide-this').hide() }})
Promises
reqwest({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo'}) .then(function (resp) { qwery('#content').html(resp.content) }, function (err, msg) { qwery('#errors').html(msg) }) .always(function (resp) { qwery('#hide-this').hide() })
reqwest({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo'}) .then(function (resp) { qwery('#content').html(resp.content) }) .fail(function (err, msg) { qwery('#errors').html(msg) }) .always(function (resp) { qwery('#hide-this').hide() })
var r = reqwest({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , success: function () { setTimeout(function () { r .then(function (resp) { qwery('#content').html(resp.content) }, function (err) { }) .always(function (resp) { qwery('#hide-this').hide() }) }, 15) }})
Options
url
a fully qualified urimethod
http method (default:GET
)headers
http headers (default:{}
)data
entity body forPATCH
,POST
andPUT
requests. Must be a queryString
orJSON
objecttype
a string enum.html
,xml
,json
, orjsonp
. Default is inferred by resource extension. Eg:.json
will settype
tojson
..xml
toxml
etc.contentType
sets theContent-Type
of the request. Eg:application/json
crossOrigin
for cross-origin requests for browsers that support this feature.success
A function called when the request successfully completeserror
A function called when the request fails.complete
A function called whether the request is a success or failure. Always called when complete.jsonpCallback
Specify the callback function name for aJSONP
request. This value will be used instead of the random (but recommended) name automatically generated by reqwest.
Security
If you are still requiring support for IE6/IE7, consider including JSON3 in your project. Or simply do the following
<script>(function () { if (!window.JSON) { document.write('<scr' + 'ipt src="http://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"><\/scr' + 'ipt>') }}());</script>
Contributing
$ git clone git://github.com/ded/reqwest.git reqwest$ cd !$$ npm install
Please keep your local edits to src/reqwest.js
. The base ./reqwest.js
and ./reqwest.min.js
will be built upon releases.
Running Tests
make test
Browser support
- IE6+
- Chrome 1+
- Safari 3+
- Firefox 1+
- Opera
Ender Support
Reqwest can be used as an Ender module. Add it to your existing build as such:
$ ender add reqwest
Use it as such:
$.ajax({ ... })
Serialize things:
$(form).serialize() // returns query string -> x=y&...$(form).serialize({type:'array'}) // returns array name/value pairs -> [ { name: x, value: y}, ... ]$(form).serialize({type:'map'}) // returns an object representation -> { x: y, ... }$(form).serializeArray()$.toQueryString({ foo: 'bar' , baz: 'thunk'}) // returns query string -> foo=bar&baz=thunk
Or, get a bit fancy:
$('#myform input[name=myradios]').serialize({type:'map'})['myradios'] // get the selected value$('input[type=text],#specialthing').serialize() // turn any arbitrary set of form elements into a query string
ajaxSetup
Use the request.ajaxSetup
to predefine a data filter on all requests. See the example below that demonstrates JSON hijacking prevention:
$.ajaxSetup({ dataFilter: function (response, type) { if (type == 'json') return response.substring('])}while(1);</x>'.length) else return response }})
RequireJs and Jam
Reqwest can also be used with RequireJs and can be installed via jam
jam install reqwest
define(function(require){ var reqwest = require('reqwest')});
spm
Reqwest can also be installed via spm
spm install reqwest
jQuery and Zepto Compatibility
There are some differences between the Reqwest way and the jQuery/Zepto way.
method
jQuery/Zepto use type
to specify the request method while Reqwest uses method
and reserves type
for the response data type.
dataType
When using jQuery/Zepto you use the dataType
option to specify the type of data to expect from the server, Reqwest uses type
. jQuery also can also take a space-separated list of data types to specify the request, response and response-conversion types but Reqwest uses the type
parameter to infer the response type and leaves conversion up to you.
JSONP
Reqwest also takes optional jsonpCallback
and jsonpCallbackName
options to specify the callback query-string key and the callback function name respectively while jQuery uses jsonp
and jsonpCallback
for these same options.
But fear not! If you must work the jQuery/Zepto way then Reqwest has a wrapper that will remap these options for you:
reqwest.compat({ url: 'path/to/data.jsonp?foo=bar' , dataType: 'jsonp' , jsonp: 'foo' , jsonpCallback: 'bar' , success: function (resp) { qwery('#content').html(resp.content) }})// or from Ender:$.ajax.compat({ ...})
If you want to install jQuery/Zepto compatibility mode as the default then simply place this snippet at the top of your code:
$.ajax.compat && $.ender({ ajax: $.ajax.compat });
Happy Ajaxing!
转载自:https://github.com/ded/reqwest
- Reqwest 使用教程
- [js]01semantic&vue&reqwest实现数据的展示
- 【教程】minicom使用教程
- 使用教程
- 【教程】InstallShield使用完全教程
- 【教程】InstallShield使用完全教程
- 【教程】InstallShield使用完全教程
- svn使用教程-终极教程
- git使用教程,命令教程
- 【教程】InstallShield使用完全教程
- exe4j使用教程,inno setup使用教程
- Xshell使用教程及WinSCP使用教程
- FTP使用教程之Filezilla使用教程
- mii-tool使用教程
- RSS的使用教程
- WinFax使用教程(图)- -
- Source Insight使用教程
- ANT 基本使用教程
- Maven使用中的一些值得注意的地方
- 将arguments转换成数组的方法
- android 集成第三方静态库的编译方法
- 蓝桥杯 入门训练 序列求和
- 我的mqtt协议和emqttd开源项目个人理解(6)
- Reqwest 使用教程
- Android 反射,动态代理
- 写在开头
- RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
- Windows下搭建基于Anaconda的Tensorflow环境
- 懂行的进,电路原理图checklist
- apache kylin 2.1.0+CDH5.8 安装部署教程
- 基于ftplib的交互式ftp客户端实现
- 套接字选项SO_LINGER