Ajax - Technical Introduction |
|
In the second step, we decided the name of the method “myMethod” to be called after receiving the response from the server. In this method, we write the code handling the response data. For example, myMethod() can have code segment as given below:
function myMethod(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var str = xmlHttp.responseText;
alert(str);
}
else {
alert(‘Request failed’);
}
}
else{
alert (‘Request failed’);
}
}
i) Checking the state of the request:
Here, readyState property of the HTTP request object “xmlHttp” is used to check the state of the request. This property can have different values indicating the state of the request.
Value |
State of the request |
0 |
Un-initialized |
1 |
Loading |
2 |
Loaded |
3 |
Interactive |
4 |
Complete |
The method above checks first the state of the request. If it is equal to 4 it means the request has completed and the response is received from the server.
ii) Checking the Status Code of the response:
The next thing that should be checked is the Status Code of the HTTP server response. The server can send different status code depending on the request processing of the server. For example:
Status |
Code Definition |
200 |
OK |
400 |
Bad Request |
401 |
Unauthorized |
404 |
Not Found |
405 |
Method Not Allowed |
500 |
Internal Server Error |
But for our purpose, status code “200” only is useful. Our method checks and if it is 200 then everything is “OK”.
|
|
Apr 2008 | Java Jazz Up |14 |
|
|
|
Pages:
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,
Download PDF |
|
|
|