Tuesday 14 July 2015

Ajax technical interview questions and answers for experienced


Ajax technical interview questions and answers for experienced


Question: How can AJAX response set a cookie?
Here are two solutions.
  1. After getting response, Make another ajax call with request where you can set the cookie.
  2. Before sending the ajax response, make the cookie in Server.
Use setcookie function, to set the cookie, like below
$cookieValue='This is cookie value';
setcookie('name', $cookieValue, time() + 3600); 



Question: How to send data in Ajax call?
$.ajax({ 
        url: '/my/site/url',
         data: { name: "Arun", location: "Chandigarh" },
         type: 'post',
         success: function(result) {
                      console.log(result);
          }
});



Question: How to submit entire form data with Ajax?
var formData = $('form#formId').serialize();
$.ajax({ 
        url: '/my/site',
         data: formData,
         type: 'post',
         success: function(result) {
                      console.log(result);
          }
});


Question: What are different readystate stages in Ajax Call?
In Ajax call, ready state have 5 values and are following:
  1. 0- The request is not initialized.
  2. 1- The request has been set up.
  3. 2- The request has been sent.
  4. 3- The request is in process.
  5. 4- The request is complete.



Question:Give a simple example of JSONP with Ajax?
$.getJSON("http://twitter.com/status/user_timeline/padraicb.json?count=10&callback=?", function(result){
   //Now result have response of ajax call
   console.log(result);
});



Question: How to URLencode a string Before sending in Ajax?
use encodeURIComponent to encode a string, For Example:
 $.ajax({
        url: '/ajax/url',
        dataType: "json",
        data: name=encodeURIComponent('Test'),
        success: function(response) {
            if(response.success == 1){
                offWords = response.data;
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
        }
    });



Question: Do AJAX requests retain PHP Session info, when calling muliple times?
Yes, PHP retain the session info over multiple ajax call.
Duration of available of php session depend of SESSION configuration.


Question: How to update the image source, if original image fails to load?
Use the onError attribute to update the image path, when failed to load.
<img alt="Image not found" onerror="this.src=&#39;show_img_when_original_failed.gif&#39;;" src="image.gif" />



Question: What is Comet in web application?
Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it


Question: How to add header in AJAX Request?
 $.ajax({
    type:"POST",
    beforeSend: function (request)
    {
        request.setRequestHeader("Header-Name", "Header Value");
    },
    url: "/ajax/url/",
    data: "name=web",
    processData: false,
    success: function(msg) {
        //Success
    }
    });



Question: How to do synchronous AJAX requests?
set async=false in ajax call.
function getRemote() {
    return $.ajax({
        type: "GET",
        url: '/ajax/url',
        async: false
    }).responseText;
}



Question: How to set all ajax call synchronous?
Set the following in after include of jQuery
$.ajaxSetup({async:false});



Question: How to send a ajax call with https when you are http page(Suppose same domain)?
Add following in header of Ajax Call.
Access-Control-Allow-Origin: http://www.domain.com



Question: How to get the error response text in Ajax?
$.ajax({
    type:"POST",
    url: "url",
    data: "name=web",    
    error: function(xhr, status, error) {
      var err = eval("(" + xhr.responseText + ")");
      console.log(err.Message);
    }
    success: function(msg) {
        //Success
    }
    });



Question: Getting all selected checkbox in an array?
var selectedIds = new array();
$("input:checkbox[name=type]:checked").each(function(){
    selectedIds.push($(this).val());
});
console.log(selectedIds);