Professional Documents
Culture Documents
JS p10
JS p10
p10
definition
In order to make an HTTP request to the server with JavaScript, you need an instance of
an object with the necessary functionality. This is where XMLHttpRequest comes in.
After making a request, you will receive a response back. At this stage, you need to tell
the XMLHttp request object which JavaScript function will handle the response
httpRequest.onreadystatechange = nameOfTheFunction;
or
httpRequest.onreadystatechange = function(){
// Process the server response here.
}
XMLHttpRequest Object Methods
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method,url,async,user,psw) Specifies the request
after declaring what happens when you receive the response, you need to actually make
the request, by calling the
open()
request.open("GET", // request method HTTP GET/POST
"data.csv”, // URL
true); // asynchronous/synchronous
1 The first parameter of the call to open() is the HTTP request method –
GET, POST, HEAD, or another method supported by your server
2 The second parameter is the URL you're sending the request to. As a
security feature, you cannot call URLs on 3rd-party domains by default.
3 The optional third parameter sets whether the request is
asynchronous. This is the first A in AJAX. true (the default).
GET or POST
GET is simpler and faster than POST, and can be used in most cases.
Method Description
setRequestHeader(header, value) Adds HTTP headers to the request
xhttp.open("GET", "ajax_test.asp", true);
Synchronous Request
The parameter to the send() method can be any data you want to send to the server if
POST-ing the request. Form data should be sent in a format that the server can parse
"name=value&anothername="+encodeURIComponent(myVar)+"&so=on"
if you want to POST data, you may have to set the MIME type of the request. For
example, use the following before calling send() for form data sent as a query string
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
Step 2 – Handling the server response
When you sent the request, you provided the name of a JavaScript
function to handle the response
httpRequest.onreadystatechange = nameOfTheFunction;
Property Description
onreadystatechange Defines a function to be called when the readyState property changes
With the XMLHttpRequest object you can define a function to be executed when the request receives an answer.
The function is defined in the onreadystatechange property of the XMLHttpResponse object
Server Response Properties
After checking the state of the request and the HTTP status code of the
response,
you can do whatever you want with the data the server sent.
You have two options to access that data:
•httpRequest.responseText – returns the server response as a string of text
•httpRequest.responseXML – returns the response as an XMLDocument
object you can traverse with JavaScript DOM functions
responseText
function start(){
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange=function(){
if (xhttp.readyState==4 && xhttp.status==200)
document.getElementById('ajax').innerHTML=xhttp.responseText;
}
xhttp.open('GET','testfile.txt',true);
xhttp.send();
}
responseXML
Method Description
getAllResponseHeader Returns all the header information from the server resource
s()
JSON
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse( httpRequest. responseText);
alert(response.computedString);
} else {
alert('There was a problem with the request.');
}}}
example
<div id="demo">
<h1>The XMLHttpRequest Object</h1>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
</script>