Skip to content

Commit c6d6264

Browse files
prashantsharmaindaffl
authored andcommitted
Fix #100 (Make ajax handle varying capitalizations on "type") (#114)
Always convert request type to upper case before testing if it's a POST or PUT request
1 parent b600958 commit c6d6264

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

ajax-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,55 @@ QUnit.asyncTest("GET requests with dataType parse JSON (#106)", function(){
5959
start();
6060
});
6161
});
62+
63+
QUnit.asyncTest("ignores case of type parameter for a post request (#100)", function () {
64+
var oldXhr = window.XMLHttpRequest || window.ActiveXObject,
65+
requestHeaders = {
66+
CONTENT_TYPE: "Content-Type"
67+
},
68+
xhrFixture = function () {
69+
this.open = function (type, url) {
70+
};
71+
72+
this.send = function () {
73+
this.readyState = 4;
74+
this.status = 200;
75+
this.onreadystatechange();
76+
};
77+
78+
this.setRequestHeader = function (header, value) {
79+
if (header === requestHeaders.CONTENT_TYPE) {
80+
var o = {};
81+
o[header] = value;
82+
this.responseText = JSON.stringify(o);
83+
}
84+
};
85+
};
86+
87+
// replace with fixture
88+
if (window.XMLHttpRequest) {
89+
window.XMLHttpRequest = xhrFixture;
90+
} else if (window.ActiveXObject) {
91+
window.ActiveXObject = xhrFixture;
92+
}
93+
94+
ajax({
95+
type: "post",
96+
url: "/foo",
97+
data: {
98+
bar: "qux"
99+
}
100+
}).then(function (value) {
101+
QUnit.equal(value[requestHeaders.CONTENT_TYPE], "application/x-www-form-urlencoded");
102+
}, function (reason) {
103+
QUnit.notOk(reason, "request failed with reason = ", reason);
104+
}).then(function () {
105+
// restore original values
106+
if (window.XMLHttpRequest) {
107+
window.XMLHttpRequest = oldXhr;
108+
} else if (window.ActiveXObject) {
109+
window.ActiveXObject = oldXhr;
110+
}
111+
start();
112+
});
113+
});

ajax.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ module.exports = namespace.ajax = function (o) {
148148
deferred.reject(e);
149149
}
150150
};
151-
var url = o.url, data = null;
152-
var isPost = o.type === "POST" || o.type === "PUT";
151+
var url = o.url, data = null, type = o.type.toUpperCase();
152+
var isPost = type === "POST" || type === "PUT";
153153
if (!isPost && o.data) {
154154
url += "?" + $._formData(o.data);
155155
}
156-
xhr.open(o.type, url);
156+
xhr.open(type, url);
157+
157158
if (isPost) {
158159
var isJson = o.dataType.indexOf("json") >= 0;
159160
data = (isJson && !o.crossDomain) ?

0 commit comments

Comments
 (0)