Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. create a new XMLHttpRequest object -- an object like any other!
- var xhr = new XMLHttpRequest();
- // 2. open the request and pass the HTTP method name and the resource as parameters
- xhr.open('GET', 'file.html', true);
- // 3. write a function that runs once the Ajax request resolves
- xhr.onload = function () {
- // 4. check if the request is done and responded with success code
- if (xhr.readyState === xhr.DONE && xhr.status >= 200 && xhr.status < 300) {
- console.log(xhr.response);
- console.log(xhr.responseText);
- // 5. insert the text sent by the server into an HTML element with id="ajax-content"
- document.getElementById('ajax-content').innerHTML = xhr.responseText;
- }
- };
- var xhr = new XMLHttpRequest();
- xhr.responseType = 'document';
- xhr.open('GET', 'file.html', true);
- xhr.onload = function () {
- if (xhr.readyState === xhr.DONE && xhr.status >= 200 && xhr.status < 300) {
- console.log(xhr.response);
- console.log(xhr.responseXML);
- // do stuff with xhr.responseXML...
- }
- };
Add Comment
Please, Sign In to add comment