Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
fetch
提交
d77810a1
F
fetch
项目概览
github
/
fetch
大约 1 年 前同步成功
通知
30
Star
25765
Fork
3216
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
fetch
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d77810a1
编写于
3月 30, 2016
作者:
D
David Graham
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support URLSearchParams POST body
https://fetch.spec.whatwg.org/#body-mixin
上级
57b7f98f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
45 addition
and
4 deletion
+45
-4
.jshintrc
.jshintrc
+2
-1
fetch.js
fetch.js
+5
-0
package.json
package.json
+4
-3
test/test-worker.html
test/test-worker.html
+1
-0
test/test.html
test/test.html
+1
-0
test/test.js
test/test.js
+32
-0
未找到文件。
.jshintrc
浏览文件 @
d77810a1
...
...
@@ -19,6 +19,7 @@
"browser": true,
"worker": true,
"globals": {
"JSON": false
"JSON": false,
"URLSearchParams": false
}
}
fetch.js
浏览文件 @
d77810a1
...
...
@@ -6,6 +6,7 @@
}
var
support
=
{
searchParams
:
'
URLSearchParams
'
in
self
,
iterable
:
'
Symbol
'
in
self
&&
'
iterator
'
in
Symbol
,
blob
:
'
FileReader
'
in
self
&&
'
Blob
'
in
self
&&
(
function
()
{
try
{
...
...
@@ -193,6 +194,8 @@
this
.
_bodyBlob
=
body
}
else
if
(
support
.
formData
&&
FormData
.
prototype
.
isPrototypeOf
(
body
))
{
this
.
_bodyFormData
=
body
}
else
if
(
support
.
searchParams
&&
URLSearchParams
.
prototype
.
isPrototypeOf
(
body
))
{
this
.
_bodyText
=
body
.
toString
()
}
else
if
(
!
body
)
{
this
.
_bodyText
=
''
}
else
if
(
support
.
arrayBuffer
&&
ArrayBuffer
.
prototype
.
isPrototypeOf
(
body
))
{
...
...
@@ -207,6 +210,8 @@
this
.
headers
.
set
(
'
content-type
'
,
'
text/plain;charset=UTF-8
'
)
}
else
if
(
this
.
_bodyBlob
&&
this
.
_bodyBlob
.
type
)
{
this
.
headers
.
set
(
'
content-type
'
,
this
.
_bodyBlob
.
type
)
}
else
if
(
support
.
searchParams
&&
URLSearchParams
.
prototype
.
isPrototypeOf
(
body
))
{
this
.
headers
.
set
(
'
content-type
'
,
'
application/x-www-form-urlencoded;charset=UTF-8
'
)
}
}
}
...
...
package.json
浏览文件 @
d77810a1
...
...
@@ -14,11 +14,12 @@
"
bower
"
:
"
1.3.8
"
,
"
chai
"
:
"
1.10.0
"
,
"
jshint
"
:
"
2.8.0
"
,
"
mocha-phantomjs
"
:
"
3.5.2
"
,
"
mocha
"
:
"
2.1.0
"
,
"
phantomjs
"
:
"
1.9.19
"
"
mocha-phantomjs
"
:
"
3.5.2
"
,
"
phantomjs
"
:
"
1.9.19
"
,
"
url-search-params
"
:
"
0.5.0
"
},
"files"
:
[
"files"
:
[
"LICENSE"
,
"fetch.js"
]
...
...
test/test-worker.html
浏览文件 @
d77810a1
...
...
@@ -7,6 +7,7 @@
</head>
<body>
<div
id=
"mocha"
></div>
<script
src=
"/node_modules/url-search-params/build/url-search-params.js"
></script>
<script
src=
"/node_modules/mocha/mocha.js"
></script>
<script>
...
...
test/test.html
浏览文件 @
d77810a1
...
...
@@ -7,6 +7,7 @@
</head>
<body>
<div
id=
"mocha"
></div>
<script
src=
"/node_modules/url-search-params/build/url-search-params.js"
></script>
<script
src=
"/node_modules/chai/chai.js"
></script>
<script
src=
"/node_modules/mocha/mocha.js"
></script>
<script>
...
...
test/test.js
浏览文件 @
d77810a1
var
support
=
{
searchParams
:
'
URLSearchParams
'
in
self
,
blob
:
'
FileReader
'
in
self
&&
'
Blob
'
in
self
&&
(
function
()
{
try
{
new
Blob
()
...
...
@@ -311,6 +312,18 @@ suite('Request', function() {
})
})
featureDependent
(
test
,
support
.
searchParams
,
'
sends URLSearchParams body
'
,
function
()
{
return
fetch
(
'
/request
'
,
{
method
:
'
post
'
,
body
:
new
URLSearchParams
(
'
a=1&b=2
'
)
}).
then
(
function
(
response
)
{
return
response
.
json
()
}).
then
(
function
(
request
)
{
assert
.
equal
(
request
.
method
,
'
POST
'
)
assert
.
equal
(
request
.
data
,
'
a=1&b=2
'
)
})
})
test
(
'
construct with url
'
,
function
()
{
var
request
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
)
assert
.
equal
(
request
.
url
,
'
https://fetch.spec.whatwg.org/
'
)
...
...
@@ -442,6 +455,25 @@ suite('Request', function() {
assert
.
equal
(
req
.
headers
.
get
(
'
content-type
'
),
'
image/png
'
)
})
featureDependent
(
test
,
support
.
searchParams
,
'
construct with URLSearchParams body sets Content-Type header
'
,
function
()
{
var
req
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
body
:
new
URLSearchParams
(
'
a=1&b=2
'
)
})
assert
.
equal
(
req
.
headers
.
get
(
'
content-type
'
),
'
application/x-www-form-urlencoded;charset=UTF-8
'
)
})
featureDependent
(
test
,
support
.
searchParams
,
'
construct with URLSearchParams body and explicit Content-Type header
'
,
function
()
{
var
req
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
headers
:
{
'
Content-Type
'
:
'
image/png
'
},
body
:
new
URLSearchParams
(
'
a=1&b=2
'
)
})
assert
.
equal
(
req
.
headers
.
get
(
'
content-type
'
),
'
image/png
'
)
})
test
(
'
clone request
'
,
function
()
{
var
req
=
new
Request
(
'
https://fetch.spec.whatwg.org/
'
,
{
method
:
'
post
'
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录