当前位置: 首页 > 工具软件 > Reqwest > 使用案例 >

reqwest请求api和约束(转载)

西门淮晨
2023-12-01

转自:https://www.oschina.net/p/reqwest

reqwest 用于浏览器异步HTTP请求。支持xmlHttpRequest, JSONP, CORS, 和 CommonJS约束。

API 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 supported
reqwest({
     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()
     }
})

约束


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
     }
})

支持的浏览器

  • IE6+

  • Chrome 1+

  • Safari 3+

  • Firefox 1+

  • Opera

转载于:https://www.cnblogs.com/mickybg/p/6015678.html

 类似资料: