FSR060523 :- Javascript Questions | AJAX | JSON

Q.s 1 :- What are the different ready states of a request in AJAX?

Q.s 2 :- What are the properties of XMLHttpRequest?

Q.s 3:- Give an example to convert JSON into javascript.

3 Likes

1st question:ready states:it is holding xmlhttp request
request response
0–req not initialised;
1–req established;
2–req received;
3–req processing;
4–request ready

2 Likes

Q.s 1 :- What are the different ready states of a request in AJAX?

0 = request not initialized
1=server connection established
2=request received
3=processing request
4=response is ready

2 Likes

Ans1:
0:Request not initialized
1:server Connection established
2:Request received
3:processig request
4:Request fiished and respose ready

2 Likes

Q.s 1 :- What are the different ready states of a request in AJAX?

0 = request not initialized
1=server connection established
2=request received
3=processing request
4=response is ready

2 Likes

var jsonData = ‘{“name”: “John Doe”, “age”: 30}’;
var obj = JSON.parse(jsonData);
console.log(obj.name);
console.log(obj.age);

1 Like

2nd question:
properties:of XMLhttp:
defining function with declaration
var a = new XMLHttpRequest;
a.open(“GET”, “https://fakestoreapi.com/products/”, true);
a.send();
a.onreadystatechange = function () {
with this.status and this.ready state}

2 Likes

0 = request not initialized
1=server connection established
2=request received
3=processing request

2 Likes

Answer-1

  1. The different ready states of a request in AJAX are:
    0 (UNSENT) - Client has been created. open() not called yet.
    1 (OPENED) - open() has been called.
    2 (HEADERS_RECEIVED) - send() has been called, and headers and status are available.
    3 (LOADING) - Downloading; responseText holds partial data.
    4 (DONE) - The operation is complete.
4 Likes

Ans 2 :-onreadystatechange
readyState
responseText
status
responseXML

2 Likes

Ans 01:-

0- Request not initialized

1- server connection established

2- request recived

3-Processing request

4- request finished and response is ready

2 Likes

3rd question:
function read_data(){
var a = new XMLHttpRequest();
a.open(“GET”,“https://jsonplaceholder.typicode.com/posts”,true);
a.send();
a.onreadystatechange=function(){
// console.log(this.readystate);
// console.log(this.status);
if(this.status == 200 && this.readyState == 4){
// console.log(typeof(this.responseText))
// document.write(this.responseText)
var main_js = JSON.parse(this.responseText);
// console.log(typeof(main_js))
console.log(main_js)

2 Likes

Answer 1:
0 : reqest not initialised;
1 : reqest established;
2 : reqest received;
3 : reqest processing;
4 : request ready;
ready states : it is holding XMLHttpRequest, request response back.

Answer 2:

var krishna = new XMLHttpRequest;
krishna.open(“GET”, “https://fakestoreapi.com/products/”, true);
krishna.send();
krishna.onreadystatechange = function () {

if (this.status == 200 && this.readyState == 4) {
…etc etc…
}
}

Answer 3:

let newTopic = () => {
    let k = new XMLHttpRequest;
    k.open('GET', 'https://fakestoreapi.com/products', true);
    k.send();
    k.onreadystatechange = function () {
        if (this.status == 200 && this.readyState == 4) {
            document.getElementsByTagName('img')[0].style.display = 'none';
            let data = JSON.parse(this.responseText);
console.log(data);
}
2 Likes
  1. The properties of XMLHttpRequest are:
  • [onreadystatechange: Stores a function (or the name of a function) to be called automatically each time the readyState property changes
  • readyState: Holds the status of the XMLHttpRequest. Changes from 0 to 4:
  • responseText: Returns the server response as a string Preformatted text
  • status: Returns the status-number of a request [4].
  • statusText: Returns the status-text (e.g. [“OK” or “Not Found”) ]
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();

3 Likes

Q.s 3:- Give an example to convert JSON into javascript.

function product(){
var x=new XMLHttpRequest();
x.open(“get”,“https://jsonplaceholder.typicode.com/posts”,true);
x.send();
x.onreadystatechangedyState =function(){
if(this.status == 200 && this.readyState==4){
var main_js = JSON.parse(this.responseText);
console.log(main_js);
}
}
}
product();

2 Likes

Ans 3: let fatarrow = () => {
let j=new XMLHttpRequest;
j.open(‘GET’,‘https://fakestoreapi.com/products’,true);
j.send();
j.onreadystatechange =function(){
if(this.status==200 && this.readyState==4){
var main_js= JSON.parse(this.responseText);
cosole.log(main_js);
}}

1 Like

Answer-3

Code-1

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();

Code-2

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);

Here, the variable text contains a JSON string. The JSON.parse() function converts this string into a JavaScript object. The resulting object is stored in the variable obj

3 Likes

ans 02-
make a function with delcration

var a = new XMLHttpRequest();

a.open(“GET”,“https://josnplaceholder.typicode.com/posts”,true);

a.send();

a.onreadystatechange = function()

2 Likes
Q1. Different States of AJAX are as follows:- 
0- not initialized Request
1-server connection established
2- request received
3-processing request
4- finished request & ready response. 
Q2. Different XML properties are:-
location.hostname
location.port
location.href
screen.availWidth
screen.availHeight
Q3. function details() {
    var a=new XMLHttpRequest();
    a.open("GET","https://jsonplaceholder.typicode.com/posts",true);
    a.send();
    a.onreadystatechange=function(){
 if(this.status==200 && this.readyState==4){
      var name=JSON.parse(this.responseText);
console.log(name);
}
details();

2 Likes

const jsonData = ‘{ “name”: “John”, “age”: 22 }’;

const obj = JSON.parse(jsonData);

console.log(obj.name);

2 Likes