Jquery JSON Interview Questions With Answers
Jquery JSON Interview Questions With Answers
com
Contents
What is Jquery ?.......................................................................................................................... 3 So will jquery replace javascript ? .............................................................................................. 3 So how do we use these reusable jquery libraries?..................................................................... 3 What is CDN (Content delivery network)? ................................................................................ 3 For Jquery files which are the popular CDNs?.......................................................................... 3 How can we reference local Jquery files if CDN fails?.............................................................. 4 What is the difference between Jquery.js and Jquery.min.js file? ............................................. 5 When should we use jquery.js over jquery.min.js ? ................................................................... 6 What is the use jquery.vsdoc.js ? ................................................................................................ 6 How does the basic syntax of Jquery looks like? ....................................................................... 6 What is the $ sign in Jquery ?.................................................................................................. 7 When should we use Jquery.noConflict()? ................................................................................. 7 What are the different ways by which you can select a HTML element in JQuery ? ................ 7 What is the use of Document.ready in Jquery ?.......................................................................... 8 Can we have two document.ready in a webpage? ...................................................................... 9 What is JSON?............................................................................................................................ 9 Do all technologies support JSON? .......................................................................................... 11 How can you make a JSON call using Jquery ? ....................................................................... 12 How can we post JSON to Server? ........................................................................................... 13 How can we post a complete HTML form in JSON format? ................................................... 13
What is Jquery ?
Jquery is a reusable javascript library which simplifies javascript coding. So rather than writing lengthy javascript code as below.
document.getElementById("txt1").value = "hello";
If you want to use Microsoft CDN you can use the below javascript.
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js"> </script>
Now if Microsoft CDN is down then the Jquery value will be undefined. So you can see in the below code we are checking if the Jquery is having undefined value then do a document write and reference your local Jquery files.
if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='Scripts/jquery.1.9.1.min.js' type='text/javascript'%3E%3C/script%3E")); }
</script>
You can also create your own jquery shortcut as shown below.
var jq = $.noConflict(); jq("p").text("I am invoked using jquery shortcut");
What are the different ways by which you can select a HTML element in JQuery ?
You can select Jquery elements in the following ways:-
Select all Below is a simple code snippet which selects all paragraph tags and hides them.
$("p").hide();
Select by ID
$("#Text1").val("Shiv");
Select using Equal method Select using Find method Select using Filter method
So we would like to execute the Jquery code which sets the textbox value only when all the HTML objects are loaded in DOM. So you can replace the code of setting text box value to something as shown below.
<script>
Below is one more example where we have attached the a function to a mouse enter event of a paragraph.
$("#p1").mouseenter(function(){ alert("You entered p1!"); });
What is JSON?
JSON (JavaScript object notation) helps us to present and exchange data in a self-descriptive, independent and light way. This data can then be easily consumed and transformed in to javascript objects. Below is a simple example of JSON format looks. You can understand from the format how lightweight and easy the format looks.
Figure :- JSON The biggest advantage of JSON format is it can be evaluated to a javascript object. For instance you can see in the below code snippet we have a JSON format data which has name,street,age and phone. Now this data can be consumed as shown in the code snippet below, evaluated to a javascript object and invoked as anobject property. You can see how we have called the name property using an object JSONObject.name.
<script type="text/javascript">
var JSONObject= { "name":"John Johnson", "street":"Oslo West 555", "age":33, "phone":"555 1234567"};
alert(JSONObject.name); </script>
Was not SOAP meant to do the same thing which JSON does?
SOAP is heavy due to XML tags. For example a SOAP message "<Name>Shiv</Name>" will become short , sweet and light in JSON like "Name" : "Shiv". Second most important it evaluates as javascript object. To convert the complicated SOAP XML in to javascript JSON object would be a tough and tedious task.
If you want your MVC to emit out JSON data you can return JsonResult as shown below. If you call the below action it will emit out Customer objects in Json format.
public JsonResult { List<Customer> obj1 = new List<Customer>(); Thread.Sleep(5000); Customer obj = new Customer(); obj.CustomerCode = "1001"; obj1.Add(obj); CustomerJson()
return Json(obj1,JsonRequestBehavior.AllowGet); }
If you want to emit JSON using ASP.NET we need to use the DataContractJsonSerializer class as shown in the below code.myPerson is the class.
DataContractJsonSerializer serializer = new DataContractJsonSerializer(myPerson.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, myPerson); string json = System.Text.Encoding.UTF8.GetString(ms.ToArray()); Response.Clear(); Response.ContentType = "application/json;charset=utf-8"; Response.Write(json); Response.End();
To make a call to the above MVC action using Jquery we need to use getJSON method. Below is the simple code for the same. It has three parameters:1. The first parameter is the URL which emits out JSON. For instance in the below code the URL is /Employee/getEmployee. 2. The next parameter helps us to pass data to the resource which emits out JSON currently its the MVC action. Currently we are only doing a get so the second parameter is NULL for now. 3. The last parameter is the call back function which will be invoked once the MVC action returns data. You can see how the getData function just displays the empcode property. Because the output is in JSON it automatically converts the JSON data to javascript object.
var mydata ={name:"Shiv",city:"Mumbai"}; $.post("/Send/Request", // URL mydata , // Data to be sent function(data,status){alert(data + + status);}); // Call back function