Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit992a191

Browse files
authored
Ajax: Don't treat array data as binary
PRgh-5197 started treating all non-string non-plain-object`data` values as binary. However, `jQuery.ajax` also supportsarrays as values of `data`. This change makes regular arraysno longer be considered binary data.Surprisingly, we had no tests for array `data` values; otherwise,we'd detect the issue earlier. This change also addsa few such missing tests.Closesgh-5203Refgh-5197
1 parente77bd9d commit992a191

File tree

2 files changed

+202
-67
lines changed

2 files changed

+202
-67
lines changed

‎src/ajax/binary.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jQuery.ajaxPrefilter( function( s, origOptions ) {
66

77
// Binary data needs to be passed to XHR as-is without stringification.
88
if(typeofs.data!=="string"&&!jQuery.isPlainObject(s.data)&&
9+
!Array.isArray(s.data)&&
910

1011
// Don't disable data processing if explicitly set by the user.
1112
!("processData"inorigOptions)){

‎test/unit/ajax.js

Lines changed: 201 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ QUnit.module( "ajax", {
397397
};
398398
});
399399

400-
ajaxTest("jQuery.ajax() - URL fragment component preservation",4,function(assert){
400+
ajaxTest("jQuery.ajax() - URL fragment component preservation",5,function(assert){
401401
return[
402402
{
403403
url:baseURL+"name.html#foo",
@@ -429,6 +429,25 @@ QUnit.module( "ajax", {
429429
},
430430
error:true
431431
},
432+
{
433+
url:baseURL+"name.html?abc#foo",
434+
data:[
435+
{
436+
name:"test",
437+
value:123
438+
},
439+
{
440+
name:"devo",
441+
value:"hat"
442+
}
443+
],
444+
beforeSend:function(xhr,settings){
445+
assert.equal(settings.url,baseURL+"name.html?abc&test=123&devo=hat#foo",
446+
"hash preserved for request with query component and array data.");
447+
returnfalse;
448+
},
449+
error:true
450+
},
432451
{
433452
url:baseURL+"name.html?abc#brownies",
434453
data:{
@@ -1489,43 +1508,92 @@ QUnit.module( "ajax", {
14891508
};
14901509
});
14911510

1492-
ajaxTest("jQuery.ajax() - JSON by content-type",5,function(assert){
1493-
return{
1494-
url:baseURL+"mock.php?action=json",
1495-
data:{
1496-
"header":"json",
1497-
"array":"1"
1511+
ajaxTest("jQuery.ajax() - JSON by content-type",10,function(assert){
1512+
return[
1513+
{
1514+
url:baseURL+"mock.php?action=json",
1515+
data:{
1516+
"header":"json",
1517+
"array":"1"
1518+
},
1519+
success:function(json){
1520+
assert.ok(json.length>=2,"Check length");
1521+
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1522+
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1523+
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1524+
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1525+
}
14981526
},
1499-
success:function(json){
1500-
assert.ok(json.length>=2,"Check length");
1501-
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1502-
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1503-
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1504-
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1527+
{
1528+
url:baseURL+"mock.php?action=json",
1529+
data:[
1530+
{
1531+
name:"header",
1532+
value:"json"
1533+
},
1534+
{
1535+
name:"array",
1536+
value:"1"
1537+
}
1538+
],
1539+
success:function(json){
1540+
assert.ok(json.length>=2,"Check length");
1541+
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1542+
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1543+
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1544+
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1545+
}
15051546
}
1506-
};
1547+
];
15071548
});
15081549

1509-
ajaxTest("jQuery.ajax() - JSON by content-type disabled with options",6,function(assert){
1510-
return{
1511-
url:url("mock.php?action=json"),
1512-
data:{
1513-
"header":"json",
1514-
"array":"1"
1515-
},
1516-
contents:{
1517-
"json":false
1550+
ajaxTest("jQuery.ajax() - JSON by content-type disabled with options",12,function(assert){
1551+
return[
1552+
{
1553+
url:url("mock.php?action=json"),
1554+
data:{
1555+
"header":"json",
1556+
"array":"1"
1557+
},
1558+
contents:{
1559+
"json":false
1560+
},
1561+
success:function(text){
1562+
assert.strictEqual(typeoftext,"string","json wasn't auto-determined");
1563+
varjson=JSON.parse(text);
1564+
assert.ok(json.length>=2,"Check length");
1565+
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1566+
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1567+
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1568+
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1569+
}
15181570
},
1519-
success:function(text){
1520-
assert.strictEqual(typeoftext,"string","json wasn't auto-determined");
1521-
varjson=JSON.parse(text);
1522-
assert.ok(json.length>=2,"Check length");
1523-
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1524-
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1525-
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1526-
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1571+
{
1572+
url:url("mock.php?action=json"),
1573+
data:[
1574+
{
1575+
name:"header",
1576+
value:"json"
1577+
},
1578+
{
1579+
name:"array",
1580+
value:"1"
1581+
}
1582+
],
1583+
contents:{
1584+
"json":false
1585+
},
1586+
success:function(text){
1587+
assert.strictEqual(typeoftext,"string","json wasn't auto-determined");
1588+
varjson=JSON.parse(text);
1589+
assert.ok(json.length>=2,"Check length");
1590+
assert.strictEqual(json[0]["name"],"John","Check JSON: first, name");
1591+
assert.strictEqual(json[0]["age"],21,"Check JSON: first, age");
1592+
assert.strictEqual(json[1]["name"],"Peter","Check JSON: second, name");
1593+
assert.strictEqual(json[1]["age"],25,"Check JSON: second, age");
1594+
}
15271595
}
1528-
};
1596+
];
15291597
});
15301598

15311599
ajaxTest("jQuery.ajax() - simple get",1,function(assert){
@@ -1573,18 +1641,36 @@ QUnit.module( "ajax", {
15731641
};
15741642
});
15751643

1576-
ajaxTest("jQuery.ajax() - data - text/plain (gh-2658)",1,function(assert){
1577-
return{
1578-
url:"bogus.html",
1579-
data:{devo:"A Beautiful World"},
1580-
type:"post",
1581-
contentType:"text/plain",
1582-
beforeSend:function(_,s){
1583-
assert.strictEqual(s.data,"devo=A%20Beautiful%20World","data is %20-encoded");
1584-
returnfalse;
1644+
ajaxTest("jQuery.ajax() - data - text/plain (gh-2658)",2,function(assert){
1645+
return[
1646+
{
1647+
url:"bogus.html",
1648+
data:{devo:"A Beautiful World"},
1649+
type:"post",
1650+
contentType:"text/plain",
1651+
beforeSend:function(_,s){
1652+
assert.strictEqual(s.data,"devo=A%20Beautiful%20World","data is %20-encoded");
1653+
returnfalse;
1654+
},
1655+
error:true
15851656
},
1586-
error:true
1587-
};
1657+
{
1658+
url:"bogus.html",
1659+
data:[
1660+
{
1661+
name:"devo",
1662+
value:"A Beautiful World"
1663+
}
1664+
],
1665+
type:"post",
1666+
contentType:"text/plain",
1667+
beforeSend:function(_,s){
1668+
assert.strictEqual(s.data,"devo=A%20Beautiful%20World","data is %20-encoded");
1669+
returnfalse;
1670+
},
1671+
error:true
1672+
}
1673+
];
15881674
});
15891675

15901676
ajaxTest("jQuery.ajax() - don't escape %20 with contentType override (gh-4119)",1,function(assert){
@@ -1633,34 +1719,82 @@ QUnit.module( "ajax", {
16331719
};
16341720
});
16351721

1636-
ajaxTest("jQuery.ajax() - data - no processing POST",1,function(assert){
1637-
return{
1638-
url:"bogus.html",
1639-
data:{devo:"A Beautiful World"},
1640-
type:"post",
1641-
contentType:"x-special-sauce",
1642-
processData:false,
1643-
beforeSend:function(_,s){
1644-
assert.deepEqual(s.data,{devo:"A Beautiful World"},"data is not processed");
1645-
returnfalse;
1722+
ajaxTest("jQuery.ajax() - data - no processing POST",2,function(assert){
1723+
return[
1724+
{
1725+
url:"bogus.html",
1726+
data:{devo:"A Beautiful World"},
1727+
type:"post",
1728+
contentType:"x-special-sauce",
1729+
processData:false,
1730+
beforeSend:function(_,s){
1731+
assert.deepEqual(s.data,{devo:"A Beautiful World"},"data is not processed");
1732+
returnfalse;
1733+
},
1734+
error:true
16461735
},
1647-
error:true
1648-
};
1736+
{
1737+
url:"bogus.html",
1738+
data:[
1739+
{
1740+
name:"devo",
1741+
value:"A Beautiful World"
1742+
}
1743+
],
1744+
type:"post",
1745+
contentType:"x-special-sauce",
1746+
processData:false,
1747+
beforeSend:function(_,s){
1748+
assert.deepEqual(s.data,[
1749+
{
1750+
name:"devo",
1751+
value:"A Beautiful World"
1752+
}
1753+
],"data is not processed");
1754+
returnfalse;
1755+
},
1756+
error:true
1757+
}
1758+
];
16491759
});
16501760

1651-
ajaxTest("jQuery.ajax() - data - no processing GET",1,function(assert){
1652-
return{
1653-
url:"bogus.html",
1654-
data:{devo:"A Beautiful World"},
1655-
type:"get",
1656-
contentType:"x-something-else",
1657-
processData:false,
1658-
beforeSend:function(_,s){
1659-
assert.deepEqual(s.data,{devo:"A Beautiful World"},"data is not processed");
1660-
returnfalse;
1761+
ajaxTest("jQuery.ajax() - data - no processing GET",2,function(assert){
1762+
return[
1763+
{
1764+
url:"bogus.html",
1765+
data:{devo:"A Beautiful World"},
1766+
type:"get",
1767+
contentType:"x-something-else",
1768+
processData:false,
1769+
beforeSend:function(_,s){
1770+
assert.deepEqual(s.data,{devo:"A Beautiful World"},"data is not processed");
1771+
returnfalse;
1772+
},
1773+
error:true
16611774
},
1662-
error:true
1663-
};
1775+
{
1776+
url:"bogus.html",
1777+
data:[
1778+
{
1779+
name:"devo",
1780+
value:"A Beautiful World"
1781+
}
1782+
],
1783+
type:"get",
1784+
contentType:"x-something-else",
1785+
processData:false,
1786+
beforeSend:function(_,s){
1787+
assert.deepEqual(s.data,[
1788+
{
1789+
name:"devo",
1790+
value:"A Beautiful World"
1791+
}
1792+
],"data is not processed");
1793+
returnfalse;
1794+
},
1795+
error:true
1796+
}
1797+
];
16641798
});
16651799

16661800
ajaxTest("jQuery.ajax() - data - process string with GET",2,function(assert){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp