forked from msatyan/NodeFullStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyAjaxLib.js
More file actions
134 lines (105 loc) · 3.29 KB
/
MyAjaxLib.js
File metadata and controls
134 lines (105 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
class MyDemo1
{
constructor()
{
}
SimpleAjax(ajaxObj, MyDataHandler, MyErrorHandler)
{
var request = $.ajax(ajaxObj);
request.done(function (data)
{
MyDataHandler(data);
MyErrorHandler( undefined );
});
request.fail(function (jqXHR, textStatus, err)
{
// just pass the jqXHR: Promise interface
MyErrorHandler(jqXHR);
MyDataHandler({});
});
}
DisplayError(jqXHR)
{
var errView = $("#MyErrorText");
if (jqXHR == undefined)
{
errView.text(' ');
if (errView.hasClass("alert-danger")) {
errView.removeClass("alert-danger");
}
errView.addClass("alert-success");
errView.css("padding", "0px");
}
else
{
errView.text(jqXHR.status + ' Error : ' + jqXHR.statusText);
if (errView.hasClass("alert-success"))
{
errView.removeClass("alert-success");
}
errView.css("padding", "10px");
errView.addClass("alert-danger");
}
}
DisplayJsonData(TheJsonData)
{
var jsonObj = undefined;
if (typeof TheJsonData === 'string' || TheJsonData instanceof String)
{
jsonObj = JSON.parse(TheJsonData);
}
else
{
jsonObj = TheJsonData;
}
var jsonPretty = JSON.stringify(jsonObj, null, ' ');
$('#MyJsonResult').empty().append(jsonPretty);
hljs.initHighlighting.called = false;
hljs.initHighlighting();
}
TrySimpleGet()
{
var ajaxObj = {
url: "/product",
method: "GET",
dataType: "json"
};
ajaxObj.url = $('#MyGetUri').val();
this.SimpleAjax(ajaxObj, DemoObj.DisplayJsonData, DemoObj.DisplayError);
}
TryAnyAction()
{
var ajaxObj = {
url: "/product",
method: "POST",
dataType: "json",
data: undefined,
};
ajaxObj.url = $('#MyActionUri').val();
var elem = document.getElementById("Action1");
ajaxObj.method = elem.innerText;
if( ajaxObj.method == "POST")
{
var elem = document.getElementById("MyInputTextarea");
var x = JSON.parse(elem.value);
console.log( x );
ajaxObj.data = x;
}
this.SimpleAjax(ajaxObj, DemoObj.DisplayJsonData, DemoObj.DisplayError);
}
}
const DemoObj = new MyDemo1();
//$(document).ready( function ()
$(() => {
// jQuery document ready
var QuickResponseStaticData = '{ "Description": "This data is from static page", "action" : "do a result refresh", "name": "Static JSON data of this page","author": { "name": "Tom", "email": "123@gmail.com", "contact": [{"location": "office", "number": 123456}, {"location": "home", "number": 987654}] } }';
//DisplayData(QuickResponseStaticData);
DemoObj.DisplayJsonData(QuickResponseStaticData);
DemoObj.DisplayError(undefined);
$('#MyInputTextarea').empty();
$('.MyActionSelectList li a').click(function(e){
var elem = document.getElementById("Action1");
elem.innerText = $(this).text();
});
});