Search in Help for developer site.

Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, 27 September 2018

Rest parameter in Javascript


What is Rest parameter in Javascript.

Rest parameter is a modern feature of Javascript that allows a function to store multiple arguments in a single array.
It is similar to param keyword in C#.

Lets understand it by an example.


function sendOrders(...allOrderIds)
{
    allOrderIds.forEach(id=>console.log(id));
}

sendOrders(100,200,300);

//output
//100 200 300


...allOrderIds is a rest parameter. In sendOrders function we are passing three integer and all will be stored in allOrderIds array.

We can send any number of parameter in Rest parameter.





Rest parameter with normal parameter:


function sendOrders(day,...allOrderIds)
{
    allOrderIds.forEach(id=>console.log(id));
}
sendOrders('Monday',100,200,300);

Rest parameter should be the last parameter in a function:


function sendOrders(...allOrderIds,day) //Error
{
    allOrderIds.forEach(id=>console.log(id));
}

Above code will throw an error saying "comma is not permitted after the rest element", because rest parameter should be last parameter.


Difference between let and var keyword in javascript

Let and Var keyword in Javascript

Let and var keywords are used to declare variable in Javascript.

When to use Let over var keyword in Javascript.

It is recommended to use Let keyword over var keyword in javascript. Lets see why

Example 1: Declaration scope

In the below example, if we use let keyword and use the variable before its declaration it gives an error. Which is absolutely fine. 
console.log(orderId);  //Gives an error
let orderId = 10;

Now if we use var keyword and try to use the variable before its declaration it doesn't give any error. Which is strange. Not Recommended.
console.log(orderId); //undefined(not an error)
var orderId = 10;

Example 2: Scope in code block

Here let variable is bound to if block
if (true) {
let orderQty = 10;
}
console.log(orderQty);  //Error, Out of scope

var keyword is not bound to if block you can use outside the if block.
if (true) { 
var orderQty = 10;
}
console.log(orderQty);  //it will print 10, No error

So it is best practice to use let keyword over var keyword for declaring variable in Javascript.

Sunday, 21 May 2017

How to overcome difficulties facing in jquery ajax call and handling it to get proper response from ASP NET MVC Action Method

Difficulties facing in  jquery  Ajax call and handling it to get proper response from server side code(MVC)


1. Overview

Most of the time you may face some challenges during jquery  ajax call ,say.. you may not get expected response from server side code, showing some internal server 500,or uncaught error right?.. So.. yes, today I am going to explain you guys how to overcome this kind of rigid situation.

Assuming that you already have a MVC application. And below line of code indicates jquery Ajax call to post employee data to server and user should expect employee data for searched parameters and obviously bind them to the model ,display on the UI. So let's begin..


function SearchEmployees() {
var empName = $('#Name :selected').val();
var JoinedOn= $("#JoinedOn").val();var url = '@Url.Action("SearchEmployees", "Home")?empName =' + empName + "&&JoinedOn=" + JoinedOn
$.ajax({
url: url,
type: 'POST',

dataType = "json";
contentType = "application/json";
success: function (result) {
$("#divEmployeeSearch").html('');
$("#divEmployeeSearch").html(result);
}
});


----------------------------------------------
 
[HttpPost]
public ActionResult SearchEmployees(string
empName , DateTime JoinedOn)
{
EmployeeData objEmployeeData = new
EmployeeData ();
BOEmployeeData  objBOEmpl = new BOEmployeeData ();
objEmployeeData .lstEmployees= objBOEmpl .SearchEmpRecords(empName , JoinedOn);
return PartialView("_EmployeesRecordSearch",
objEmployeeData);
}
 


So, in above code you can observe that we are searching employees through Ajax call by passing required parameters to the action method which in turn returns employees list data which matches the passed parameters.

Ok now if you run the application and  pass the parameters to the server, you should expect employees results on your screen right? But I say obviously you will not. Because as we have written bad line of code ,so we wont get employee data on UI.
Lets find and figure out the code which causing the error.


In  Ajax call we mentioned like ,
dataType = "json";
contentType = "application/json";


So,it clearly indicates that we are expecting result in Json data type..but what we are actually returning?did you observe?..

return PartialView("_EmployeesRecordSearch", objEmployeeData);

In above line you can observe, we are returning PartialView which contains list of employees. So what  we are expecting server to return data is not at all matching what actually server method is returning.

Solution:
So, this may result in uncaught error or some internal server 500 error. To overcome this exception, simply remove below line of code from your Ajax call
.
dataType = "json";
contentType = "application/json";

That's it. It should work :)


Note:

1. In Ajax call name of the parameters which you are sending to server method should match with server method parameters. Say, name and data type.

2.If you design server method in such a way that parameters should accept null values also, you should declare them as null able parameters otherwise Ajax call may fail if you pass null values to the method.


Thank you friends.:)
Please feel free to ask your doubts and questions. I will be glad to help you all. And Hope this article will help you guys.