0
1
Fork 0
hedera-web-mindshore/js/vn/json-request.js

89 lines
1.8 KiB
JavaScript
Raw Normal View History

/**
* Handler for JSON rest requests.
**/
Vn.JsonRequest = new Class
({
initialize: function (methodName)
{
this._methodName = methodName;
}
2016-07-22 20:00:27 +00:00
,sendWithUrl: function (params, callback, method, url)
{
var request = new XMLHttpRequest ();
2016-07-22 20:00:27 +00:00
request.open (method, url, true);
request.setRequestHeader ('Content-Type',
'application/x-www-form-urlencoded');
request.onreadystatechange =
this._onStateChange.bind (this, request, callback);
request.send (Vn.Url.makeUri (params));
}
2016-07-22 20:00:27 +00:00
,send: function (params, callback)
{
2016-08-22 10:41:05 +00:00
var url = 'json.php?method='+ encodeURIComponent (this._methodName);
2016-07-22 20:00:27 +00:00
this.sendWithUrl (params, callback, 'post', url);
}
,_onStateChange: function (request, callback)
{
if (request.readyState !== 4)
return;
2016-08-22 10:41:05 +00:00
var data = null;
var error = null;
try {
var json = JSON.parse (request.responseText);
var jsData = json.data;
2016-08-22 10:41:05 +00:00
var jsWarns = json.warnings;
if (request.status == 200)
{
data = jsData;
}
else
{
var ex = new Vn.JsonException ();
ex.exception = jsData.exception;
ex.message = jsData.message;
ex.code = jsData.code;
ex.file = jsData.file;
ex.line = jsData.line;
ex.trace = jsData.trace;
throw ex;
}
}
catch (e)
{
2016-08-22 10:41:05 +00:00
data = null;
error = e;
}
if (callback)
2016-08-22 10:41:05 +00:00
callback (this, data, error);
}
,sendForm: function (form, callback)
{
var params = {};
var elements = form.elements;
for (var i = 0; i < elements.length; i++)
if (elements[i].name)
params[elements[i].name] = elements[i].value;
2016-07-22 20:00:27 +00:00
this.sendWithUrl (params, callback, form.method, form.action);
}
2016-08-25 10:47:09 +00:00
,sendFormMultipart: function (form, callback)
{
var request = new XMLHttpRequest ();
request.open (form.method, form.action, true);
request.onreadystatechange =
this._onStateChange.bind (this, request, callback);
request.send (new FormData (form));
}
});