add postData to get data

This commit is contained in:
2024-12-08 15:24:40 +06:00
parent 91e8b21d39
commit ddcb146952
9 changed files with 585 additions and 393 deletions

View File

@ -1,7 +1,22 @@
/*jshint esversion: 6 */
"use strict";
//var g_translations = {'':''};
function strToInt(str){
if(str==null) return null;
const match = str.trim().match(/[-+]?\d+(\.\d+)?/);
if (match) {
const number = Number(match[0]);
return isNaN(number) ? null : number;
}
return null;
}
function removeChild(parent){
if(parent==null) return;
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
//Массив g_translations подгружается отдельно
function trt(key)
@ -237,7 +252,7 @@ function loadContent(url,obj)
req.send( null );
}
//POST Json Data to server and Json in result
//POST Json Data to server
function postJsonData(url,data,fun){
if(typeof data !== 'string') {
data = JSON.stringify(data);
@ -248,15 +263,21 @@ function postJsonData(url,data,fun){
return function(){
if(req.readyState == 4 || typeof(req.readyState)=='undefined'){
if(req.status == 200) {
let json = null;
try {
json = JSON.parse(req.responseText);
} catch (e) {
if(req.responseXML!=null) {
let node = req.responseXML.documentElement;
node.error_code='0';
fun(true, node);
}else {
let json = null;
try {
json = JSON.parse(req.responseText);
} catch (e) {
}
if (json != null)
fun(true, json);
else
fun(false, req.responseText);
}
if (json != null)
fun(true, json);
else
fun(false, req.responseText);
}else{
fun(false,trt('Failed_to_receive_data'));
}
@ -267,6 +288,45 @@ function postJsonData(url,data,fun){
req.setRequestHeader("Content-type", "application/json");
req.send(data);
}
//POST Json Data to server
function postXMLData(url,data,fun){
if(typeof data !== 'string') {
let serializer = new XMLSerializer();
data = serializer.serializeToString(data);
}
let req=createRequestObject();
req.onreadystatechange = function(req)
{
return function(){
if(req.readyState == 4 || typeof(req.readyState)=='undefined'){
if(req.status == 200) {
if(req.responseXML!=null) {
let node = req.responseXML.documentElement;
node.error_code='0';
fun(true, node);
}else {
let json = null;
try {
json = JSON.parse(req.responseText);
} catch (e) {
}
if (json != null)
fun(true, json);
else
fun(false, req.responseText);
}
}else{
fun(false,trt('Failed_to_receive_data'));
}
}
};
}(req);
req.open( "POST", url, true );
req.setRequestHeader("Content-type", "application/xml");
req.send(data);
}
//Вывести текст поверх окон с кнопочкой OK
function alert2(title,smallText,fullText,okFunc=null)
@ -278,7 +338,10 @@ function alert2(title,smallText,fullText,okFunc=null)
}
let pos1=smallText.indexOf('[[');
let pos2=smallText.indexOf(']]');
if(pos1>=0 && pos2>=0 && pos1<pos2) smallText=smallText.substring(pos1+2, pos2);
if(pos1>=0 && pos2>=0 && pos1<pos2) {
fullText = smallText;
smallText = smallText.substring(pos1 + 2, pos2);
}
let win=new TWin(true);
win.BuildGUI(10,10);
@ -1306,7 +1369,7 @@ alert(JSON.stringify(xmlHttpRequest));
}*/
};
/** Класс асинхронных запросов к серверу
/** Класс асинхронных запросов к серверу (TODO удалить его и не использовать)
*/
class myXMLHttpRequest
{