JSON.parse() is supported on all modern browsers and a far better
solution than the hakish and potentially dangerous eval().
Also calculate the duration of request and pass it as 3rd argument to the
callback function, this makes it easier to calculate request delays or
poll intervals in code using XHR.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
{
this.reinit();
+ var ts = Date.now();
var xhr = this._xmlHttp;
var code = this._encode(data);
var json = null;
if (xhr.getResponseHeader("Content-Type") == "application/json") {
try {
- json = eval('(' + xhr.responseText + ')');
+ json = JSON.parse(xhr.responseText);
}
catch(e) {
json = null;
}
}
- callback(xhr, json);
+ callback(xhr, json, Date.now() - ts);
}
}
{
this.reinit();
+ var ts = Date.now();
var xhr = this._xmlHttp;
var code = this._encode(data);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
- callback(xhr);
+ callback(xhr, null, Date.now() - ts);
}
xhr.open('POST', url, true);