Difference between revisions of "Ajax"

From rbachwiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 18: Line 18:
* 3 Interacting
* 3 Interacting
* 4 Complete
* 4 Complete
'''[[ Code for APP| Sample Code]]'''
== Post Data ==
'''[[Code_Post| Code for Post]]'''


==Get Data ==
==Get Data ==

Latest revision as of 17:31, 24 December 2016

Setting Up Ajax Request

1. Setup the Request

var httpRequest = new XMLHttpRequest();

2. Method type

var method = 'GET'

3. URL or location of local file

-var url = 'filename.txt'
-var url = 'https://jsonplaceholder.typicode.com/posts';

4. Open the connection

httpRequest.open(method, url, true)

The 'true' argument is whether its asynchronous, other arguments that can be passed are Usernames and Passwords.


Ready States Codes

  • 0
  • 1 Loading
  • 2 Loaded
  • 3 Interacting
  • 4 Complete

Sample Code

Post Data

Code for Post

Get Data

var xhttp = new XMLHttpRequest();
var url = 'https://jsonplaceholder.typicode.com/posts';
var method = 'GET';
xhttp.open(method, url);

xhttp.onreadystatechange = function(){
  if(xhttp.readyState == 4 && xhttp.status == 200){
    console.log(JSON.parse(xhttp.responseText));
    
  } else if(xhttp.readyState === XMLHttpRequest.DONE && xhttp.status !== 200)
  {
    console.log(xhttp.status);
  }
  
};

xhttp.send();

Back To Top- Home - Category