Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
8e3b50c5
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
8e3b50c5
编写于
4月 08, 2018
作者:
J
Justin Beckwith
提交者:
GitHub
4月 08, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: es6ify the docs a little (#1461)
上级
7b11cc71
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
34 addition
and
35 deletion
+34
-35
COOKBOOK.md
COOKBOOK.md
+22
-23
README.md
README.md
+12
-12
未找到文件。
COOKBOOK.md
浏览文件 @
8e3b50c5
...
...
@@ -11,12 +11,12 @@ $ npm install axios promise --save
```
```
js
var
axios
=
require
(
'
axios
'
);
const
axios
=
require
(
'
axios
'
);
require
(
'
promise/polyfill-done
'
);
axios
.
get
(
'
http://www.example.com/user
'
)
.
then
(
function
(
response
)
{
.
then
(
(
response
)
=>
{
console
.
log
(
response
.
data
);
return
response
;
})
...
...
@@ -30,16 +30,16 @@ $ npm install axios promise.prototype.finally --save
```
```
js
var
axios
=
require
(
'
axios
'
);
const
axios
=
require
(
'
axios
'
);
require
(
'
promise.prototype.finally
'
).
shim
();
axios
.
get
(
'
http://www.example.com/user
'
)
.
then
(
function
(
response
)
{
.
then
(
(
response
)
=>
{
console
.
log
(
response
.
data
);
return
response
;
})
.
finally
(
function
()
{
.
finally
(
()
=>
{
console
.
log
(
'
this will always be called
'
);
});
```
...
...
@@ -52,19 +52,19 @@ $ npm install axios pako --save
```
js
// client.js
var
axios
=
require
(
'
axios
'
);
var
pako
=
require
(
'
pako
'
);
const
axios
=
require
(
'
axios
'
);
const
pako
=
require
(
'
pako
'
);
var
user
=
{
const
user
=
{
firstName
:
'
Fred
'
,
lastName
:
'
Flintstone
'
};
var
data
=
pako
.
deflate
(
JSON
.
stringify
(
user
),
{
to
:
'
string
'
});
const
data
=
pako
.
deflate
(
JSON
.
stringify
(
user
),
{
to
:
'
string
'
});
axios
.
post
(
'
http://127.0.0.1:3333/user
'
,
data
)
.
then
(
function
(
response
)
{
.
then
(
(
response
)
=>
{
response
.
data
=
JSON
.
parse
(
pako
.
inflate
(
response
.
data
,
{
to
:
'
string
'
}));
console
.
log
(
response
.
data
);
return
response
;
...
...
@@ -73,25 +73,24 @@ axios
```
js
// server.js
var
pako
=
require
(
'
pako
'
);
var
http
=
require
(
'
http
'
);
var
url
=
require
(
'
url
'
);
var
server
;
const
pako
=
require
(
'
pako
'
);
const
http
=
require
(
'
http
'
);
const
url
=
require
(
'
url
'
);
server
=
http
.
createServer
(
function
(
req
,
res
)
{
const
server
=
http
.
createServer
((
req
,
res
)
=>
{
req
.
setEncoding
(
'
utf8
'
);
var
parsed
=
url
.
parse
(
req
.
url
,
true
);
var
pathname
=
parsed
.
pathname
;
const
parsed
=
url
.
parse
(
req
.
url
,
true
);
const
pathname
=
parsed
.
pathname
;
if
(
pathname
===
'
/user
'
)
{
var
data
=
''
;
req
.
on
(
'
data
'
,
function
(
chunk
)
{
let
data
=
''
;
req
.
on
(
'
data
'
,
(
chunk
)
=>
{
data
+=
chunk
;
});
req
.
on
(
'
end
'
,
function
()
{
var
user
=
JSON
.
parse
(
pako
.
inflate
(
data
,
{
to
:
'
string
'
}));
req
.
on
(
'
end
'
,
()
=>
{
const
user
=
JSON
.
parse
(
pako
.
inflate
(
data
,
{
to
:
'
string
'
}));
console
.
log
(
user
);
res
.
writeHead
(
200
,
{
...
...
@@ -115,9 +114,9 @@ $ npm install jsonp --save
```
```
js
var
jsonp
=
require
(
'
jsonp
'
);
const
jsonp
=
require
(
'
jsonp
'
);
jsonp
(
'
http://www.example.com/foo
'
,
null
,
function
(
err
,
data
)
{
jsonp
(
'
http://www.example.com/foo
'
,
null
,
(
err
,
data
)
=>
{
if
(
err
)
{
console
.
error
(
err
.
message
);
}
else
{
...
...
README.md
浏览文件 @
8e3b50c5
...
...
@@ -188,7 +188,7 @@ You can create a new instance of axios with a custom config.
##### axios.create([config])
```
js
var
instance
=
axios
.
create
({
const
instance
=
axios
.
create
({
baseURL
:
'
https://some-domain.com/api/
'
,
timeout
:
1000
,
headers
:
{
'
X-Custom-Header
'
:
'
foobar
'
}
...
...
@@ -424,7 +424,7 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
```
js
// Set config defaults when creating the instance
var
instance
=
axios
.
create
({
const
instance
=
axios
.
create
({
baseURL
:
'
https://api.example.com
'
});
...
...
@@ -439,7 +439,7 @@ Config will be merged with an order of precedence. The order is library defaults
```
js
// Create an instance using the config defaults provided by the library
// At this point the timeout config value is `0` as is the default for the library
var
instance
=
axios
.
create
();
const
instance
=
axios
.
create
();
// Override timeout default for the library
// Now all requests using this instance will wait 2.5 seconds before timing out
...
...
@@ -478,14 +478,14 @@ axios.interceptors.response.use(function (response) {
If you may need to remove an interceptor later you can.
```
js
var
myInterceptor
=
axios
.
interceptors
.
request
.
use
(
function
()
{
/*...*/
});
const
myInterceptor
=
axios
.
interceptors
.
request
.
use
(
function
()
{
/*...*/
});
axios
.
interceptors
.
request
.
eject
(
myInterceptor
);
```
You can add interceptors to a custom instance of axios.
```
js
var
instance
=
axios
.
create
();
const
instance
=
axios
.
create
();
instance
.
interceptors
.
request
.
use
(
function
()
{
/*...*/
});
```
...
...
@@ -532,8 +532,8 @@ You can cancel a request using a *cancel token*.
You can create a cancel token using the
`CancelToken.source`
factory as shown below:
```
js
var
CancelToken
=
axios
.
CancelToken
;
var
source
=
CancelToken
.
source
();
const
CancelToken
=
axios
.
CancelToken
;
const
source
=
CancelToken
.
source
();
axios
.
get
(
'
/user/12345
'
,
{
cancelToken
:
source
.
token
...
...
@@ -558,8 +558,8 @@ source.cancel('Operation canceled by the user.');
You can also create a cancel token by passing an executor function to the
`CancelToken`
constructor:
```
js
var
CancelToken
=
axios
.
CancelToken
;
var
cancel
;
const
CancelToken
=
axios
.
CancelToken
;
let
cancel
;
axios
.
get
(
'
/user/12345
'
,
{
cancelToken
:
new
CancelToken
(
function
executor
(
c
)
{
...
...
@@ -583,7 +583,7 @@ By default, axios serializes JavaScript objects to `JSON`. To send data in the `
In a browser, you can use the
[
`URLSearchParams`
](
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
)
API as follows:
```
js
var
params
=
new
URLSearchParams
();
const
params
=
new
URLSearchParams
();
params
.
append
(
'
param1
'
,
'
value1
'
);
params
.
append
(
'
param2
'
,
'
value2
'
);
axios
.
post
(
'
/foo
'
,
params
);
...
...
@@ -594,7 +594,7 @@ axios.post('/foo', params);
Alternatively, you can encode data using the
[
`qs`
](
https://github.com/ljharb/qs
)
library:
```
js
var
qs
=
require
(
'
qs
'
);
const
qs
=
require
(
'
qs
'
);
axios
.
post
(
'
/foo
'
,
qs
.
stringify
({
'
bar
'
:
123
}));
```
...
...
@@ -617,7 +617,7 @@ axios(options);
In node.js, you can use the
[
`querystring`
](
https://nodejs.org/api/querystring.html
)
module as follows:
```
js
var
querystring
=
require
(
'
querystring
'
);
const
querystring
=
require
(
'
querystring
'
);
axios
.
post
(
'
http://something.com/
'
,
querystring
.
stringify
({
foo
:
'
bar
'
}));
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录