req.responseType = "json";
req.onreadystatechange = function onReadyStateChange () {
if (req.readyState == XMLHttpRequest.DONE) {
+ // hack for IE/Edge support (req.responseType == "json" is not supported)
+ var respType = req.responseType;
+ var resp = req.response;
+
+ if (respType === "") {
+ try {
+ resp = JSON.parse(req.response);
+ respType = "json";
+ } catch(e) {
+ console.log("Newslash.post: cannot parse JSON");
+ }
+ }
+
// request done
- if (req.status == 200 && req.responseType == "json") {
- return resolve(req.response);
+ if (req.status == 200 && respType == "json") {
+ return resolve(resp);
}
+
// request failed
- if (req.responseType == "json") {
- return reject(req.response);
+ if (respType == "json") {
+ return reject(resp);
}
return reject(req);
}
req.responseType = "json";
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
+ // hack for IE/Edge support (req.responseType == "json" is not supported)
+ var respType = req.responseType;
+ var resp = req.response;
+
+ if (respType === "") {
+ try {
+ resp = JSON.parse(req.response);
+ respType = "json";
+ } catch(e) {
+ console.log("Newslash.post: cannot parse JSON");
+ }
+ }
+
// request done
- if (req.status == 200 && req.responseType == "json") {
- return resolve(req.response);
+ if (req.status == 200 && respType == "json") {
+ return resolve(resp);
}
+
// request failed
- if (req.responseType == "json") {
- if (req.response && req.response.reason == "invalid_csrf_token") {
+ if (respType == "json") {
+ if (resp && resp.reason == "invalid_csrf_token") {
if (retryCount >= 3) {
return reject({error: 1, reason: "exceeded retry count"});
}
- var newToken = req.response.csrf_token;
+ var newToken = resp.csrf_token;
if (newToken) {
data.csrf_token = newToken;
// retry with new token
return 1;
}
}
- return reject(req.response);
+ return reject(resp);
}
return reject(req);
}