Skip to content

Commit 11c466a

Browse files
authored
ajax: set application/json request header accordingly (#397)
1 parent 218f2c7 commit 11c466a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

[web]/ajax/ajax.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ function decode( uri ) {
2626
return uri;
2727
}
2828

29+
// Check data is JSON
30+
function isJSON(str) {
31+
try {
32+
JSON.parse(str);
33+
} catch (e) {
34+
return false;
35+
}
36+
return true;
37+
}
2938

3039
function AJAXRequest( method, url, data, process, async, dosend) {
3140
var self = this;
@@ -84,7 +93,7 @@ function AJAXRequest( method, url, data, process, async, dosend) {
8493
}
8594

8695
self.AJAX.open(method, url, async);
87-
96+
8897
if (method == "POST") {
8998
//self.AJAX.setRequestHeader("Connection", "close"); // seems to cause issues in IE
9099
self.AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
@@ -96,6 +105,9 @@ function AJAXRequest( method, url, data, process, async, dosend) {
96105
// you'd do this to set special request headers
97106
if ( dosend || typeof dosend == 'undefined' ) {
98107
if ( !data ) data="";
108+
if (method == "POST" && isJSON(data)) // change content type
109+
self.AJAX.setRequestHeader("Content-Type", "application/json");
110+
99111
self.AJAX.send(data);
100112
}
101113
return self.AJAX;

0 commit comments

Comments
 (0)