I will be breaking down the post in to two sections.
First I will mention how to accomplish above mentioned task using handlers and I will then go on and discuss the underlying concepts and basic theories for the beginners in the next section.


How to get it done!!!


What are the core parts we will need to perform a cross domain request using JavaScript?

  •     A Mechanism to call the URL that sends data to the calling page.
  •     A padded JSON object (or a JSONP object)
    •    If the calling URL don’t support cross domain calls, functionality to wrap or pad the returned JSON object with a call back function.
  •    A method to handle the call back.
OK! Let’s take this one step at a time.
In this example I will be using JQuery AJAX facilities to call the URL in the other domain.


jQuery.ajax
({
url: "http://www.crosseddomain.com/testPage.html,
dataType: "jsonp",
type: "GET",
cache: true,
jsonpCallback: 'handleResponse'
});

jsonpCallback option allows us to specify the call back method which will handle the returned data upon the response is received.
We specify cache as true to avoid the random time stamp value JQuery appends on the request.
dataType is set to jsonp so the call back function will be added to the request.
When the entire configuration is done correctly the page will send a request like http://www.crosseddomain.com/testPage.html&callback=handleResponse through the network.
Now all we have to do is write a function to handle the returned value, the function name should be the value we passed for the jsonpCallback option
function handleResponse(response) {
//your custom logic goes here
}

That will work only if the testPage.html wraps the response with the call back. Or in other words if the requested page is responsible for making the request cross domain compatible by converting the JSON object in to a JSONP object by adding the call back function around it.
What shall we do when we are given a URL that does not support cross domain script calls? How to make it JSONP compatible?


Normal JSON objects will not work across cross domains because of the same origin policy, which restricts browser side programming languages from accessing scripts in another site or a domain. (I will talk about these theory parts in detail on my next blog, so stay tuned).
So obviously it does not restrict server side programming languages like C# from accessing URLs in separate domains. 


Keeping that in mind, we can send the request for the URL from server side, after receiving the normal response we can wrap it with the call back function and we can send it to the page where the call originated from.


To get this done we will use a generic web handler or an ashx file.


For demonstration purposes, the handler will be expecting some string data from the requesting page and it will send a dummy JSON object that contains user inputted data with some custom data from the server side. (To make things simple I will not add the code to do an actual web request, rather I will just hard code the JSON string).


I will add a text box to the page and will get the user input in each key press. After a response is received the call back function will display retrieved data in a label.


The HTML markup will be 

    <div>
        Enter your text : <input type="text" id="suggest" name="Suggestion" />
    div>
    <label id="results">
    label>

And the JQuery request will look something like this.

         $(document).ready(function () {
            $("#suggest").keyup(function () {           
                var data = $("#suggest").val();
                jQuery.ajax({
                    url: "http://localhost:18395/RequestHandler.ashx?data=" + data,
                    dataType: "jsonp",
                    type: "GET",
                    cache: true,
                    jsonpCallback: 'handleResponse'
                });

            });
        });

The RequestHandler.ashx file code is mentioned below.

    public class RequestHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var data = context.Request.QueryString["data"];
            var callback = context.Request.QueryString["callback"];
            var response = context.Response;
            response.Clear();
            response.ContentType = "application/json";
            var sampleJson = "{\"firstName\":\"Dhanushka\",\"lastName\":\"Athukorala\",\"dataReceived\":\"" + data + "\"}";
            var paddedResponse = string.Format("{0}({1})", callback, sampleJson);
            response.Write(paddedResponse);
            response.Flush();
        }
       
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

So let’s see what’s happening in the code behind file.
  • We retrieve data passed from the user from the query string (data variable)
  • Call back function name is also retrieved from the query string (callback variable)
  • A JSON object is created (this is the line where you have to call a URL and get data, you can use the functionalities implemented in HttpWebRequest, HttpWebResponse to accomplish this)
  • We wrap the final JSON object with the call back function so it will be in the format callback(JSON) making it a JSONP object (paddedResponse variable) 
  • The JSONP object is written to the response making it available for the calling page.
Now when the response is flushed it will return back to the calling page. Here the script tag of that page will execute received data or the JSONP object, or in other words it will call the call back method with a JSON object.

The code for our call back method is mentioned below.

        function handleResponse(response) {
            var data = eval(response);
            var firstName = data.firstName;
            var lastName = data.lastName;
            var sentData = data.dataReceived;
            $("#results").html("First Name : " + firstName + "
Last Name : "
+ lastName + "
Sent Data : "
+ sentData);
        }

This call back method will process the returned JSON string and will print the containing information in the results label

So in a nut shell the codes we need are

Script tag and the HTML

Handler code

And the out put will be 

Hope it helps.

I will explain the underlying principals of cross domain communication with scripts in the second part of this blog.

Cheers!!


I got my blogger hat on after a very long time. With all the week end lectures and office works cramping up my schedule, there was virtually no time left for me to blog about anything. I came across pretty interesting topics and subjects over my past few non blogging months and will try to share them with you in the coming days.
Ok let’s get started…

Let’s say we are writing a SQL query to retrieve the names of the customers who are above 20, we will write something like this

select cust.name
from customers cust
where cust.age>20

But when it comes to LINQ, as we all know the syntax is totally inverted.
The same query will look like this when we do it on a list of Customer objects.

IEnumerable<string> olderCustomersQuery = from cust in customers
                                          where cust.Age > 20
                                          select cust.Name;


But WHY!!!

To understand we need to get back to basics.

There are two ways to build up a LINQ expression, Extension methods and Query operators. The one mentioned above falls in to the latter part.
Let’s see how to write the same example using extension methods.

IEnumerable<string> olderCustomersExtension = customers
                                              .Where(cust => cust.Age > 20)
                                              .Select(cust => cust.Name);


All these extension methods are available on any class that implements the generic IQueryable or IEnumerable<T> interfaces.


So what’s up with the ordering of the method calls, why do we need to call the “Where” extension method before calling “Select”.

The answer is simple.

Where filters data that are returned from a data source.
While Select narrows down the information that shown in the final projection by specifying which properties to retrieve.

So when I say customers.Select(cust => cust.Name) it just gives me a list of strings so customers.Select(cust => cust.Name).Where(cust => cust.Age > 20) will not be possible because now the Where clause don’t get a customer object to query on.


Because of that when we are writing LINQ expressions we should use the extension methods in the correct order, otherwise you may get unexpected query results or compilation errors.

Although the LINQ extension methods and types that are defined in System.Linq namespace are very powerful, the syntax can be quite cumbersome, and it is easy to introduce subtle errors that are difficult to spot and correct. 
To avoid these drawbacks designers of C# have provided an alternative approach to the use of LINQ, through a series of LINQ query operators that are part of the C# language.

There is an equivalent query operator for each LINQ extension method.

Because these query operators are built to compliment the functionality of the equalant extension methods. They should also follow the above mentioned practices and rules to avoid any kind of side effects on the data that will be retrieved by the LINQ expression.

Hope this clears things up!!