-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
97 lines (87 loc) · 2.79 KB
/
ajax.js
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
/***********************************************************
*
* Lib Ajax - [email protected]
*
***********************************************************/
/**
* Creation d'un objet ajax
*/
function AjaxCreate()
{
var request = false;
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (err3) {
try {
request = new XMLHttpRequest();
}
catch (err1)
{
request = false;
}
}
}
return request;
}
/**
* * Requete POST avec action de retour
* */
function AjaxPost(element, url, data, funcPrinting, funcLoading)
{
var xhr = AjaxCreate();
xhr.onreadystatechange=function()
{
/* Si reponse recu */
if(xhr.readyState == 4)
{
/* On stop le decompte du timeout */
clearTimeout(timeout);
/* On enleve la notification de chargement */
if(funcLoading != false)
{
funcLoading(element, false);
}
if(xhr.status == 200)
{
if(funcLoading != false)
{
funcLoading(element, "0");
}
var content = xhr.responseText;
/* Affichage de la reponse */
funcPrinting(content, element);
}
}
else
{
/* On affiche la notification de chargement */
if(funcLoading != false)
{
funcLoading(element, true);
}
}
};
xhr.open("POST", url , true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded charset=utf-8");
xhr.send(data);
/* Gestion timeout */
var timeout = setTimeout(function() {funcPrinting("<div id=\"error\">Ajax : Le serveur ne répond pas.</div>", element);xhr.abort();}, 20000);
}
/**
* * Fonctions d'affichage
* */
/* Affichage d'un contenu HTML */
function storing(data, element)
{
element.innerHTML = data;
}
/* Affichage d'une chaine de caracteres */
function writing(data, element)
{
element.value = data;
}