Getting cross domain Ajax to work!

Last week I was working on a project which required me to use ajax to fetch the results from 3 different servers and use the result together to have a comparison side-by-side.

I had a little knowledge of Ajax, so I had to strive a bit for getting things to work. Here, I share my findings.

 

Problem: Using Ajax to fetch cross domain data.

What does it mean?

Suppose my current domain is blahblah.com and I want something from phewphew.com then I need to do a cross domain fetch with Ajax.

By conventions Javascript doesn't allow to fetch such cross domain data. It involves security issues. Anyways, I didn't go much into that !!

Quick solution 1 :

One solution to this problem is to use a custom servlet ( or php, whatever you use, it will act as a proxy ) /JSP page that downloads the JSON ( or any other type ) and simply reproduces it.  In that case, you will put your Ajax calls to a page on same domain and that will work perfectly fine.

 

Solution 2 :

Another solution, if using JQueries, using JSONP would help retrieving JSON data easily.

 

NOTE: These were really abstract solutions, I would try to illustrate it with example code soon .

 

 

 

 

How To: Successfully set up Amazon EC2 micro instance

Amazon Web Services is a very rich arsenal of web services. Surprisingly, using them is really easy and simple. I tried my first hands on Amazon's Elastic Cloud, in layman's terms, it is a server
I tried to set up a AWS micro instance on Amazon EC2. The setup is very simple and we can set a system running in just few minutes. I could also quickly set up a tomcat server running on it.
I will quickly list down the steps that I followed.

(1) From EC2 console launch a new Instance. For quick start select any AMI ( Amazon Machine Image) that suits your need. It can later be changed or upgraded.

(2) Once an AMI is selected, fill in the details related to instances (Micro instance's usage falls under free tier for the first year), kernel, ram, monitoring details etc.

(3) Next, create a key-value (optional)

(4) Select proper security group. (This is also important to keep your instance secure. Once I got my instance hacked due to careless firewall settings ). Don't allow many ports to be open, only the few that you intend to use for your need.

(5) Review everything and click 'Launch' and done!! Your instance will be ready to use within few minutes.
(6) Create a new key-pair and download the private file.

(7) Once the status of the instance turns 'running' it can be used to connect.

$ ssh -i aws.pem ec2-user@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
(8) This is it!! Time to have fun!!





A note on dictionaries in Programming languages

I recently started with learning python. The good thing about python is that everything in python is dynamic. Python is dynamically typed language. What does it means?
In C/C++ or Java we need to define the type of a variable before using it.
int a; 
float b;
// even Objects need to be defined in
Matrix m1;

But, python is different in this regard. Here, we can use a variable and its type is decided on first assignment.

list  # not needed but just for explanation

list = [] # creates an empty list


Thus, Python decides the type of a variable on its first assignment making it dynamically typed. But, another thing to be careful with in it is that Python is also strongly typed language. Means, once assigned we cant change the type of that variable. In, above python code once list varible is assigned an list object it cant be assigned any other data type. It remains list.


Below, is the summarization of different types of languages based on the property we just discussed.

statically typed language

A language in which types are fixed at compile time. Most statically typed languages enforce this by requiring you to declare all variables with their datatypes before using them. Java and C are statically typed languages.

dynamically typed language

A language in which types are discovered at execution time; the opposite of statically typed. VBScript and Python are dynamically typed, because they figure out what type a variable is when you first assign it a value.

strongly typed language

A language in which types are always enforced. Java and Python are strongly typed. If you have an integer, you can't treat it like a string without explicitly converting it.

weakly typed language

A language in which types may be ignored; the opposite of strongly typed. VBScript is weakly typed. In VBScript, you can concatenate the string '12' and the integer 3 to get the string '123', then treat that as the integer 123, all without any explicit conversion.


line-by-line processing : Linux Bash

Often we require to process a file content line by line. Linux inherently supports this thorough its powerful shell commands. Lets walk through some of the ways to parse a file line-by-line.



First, we can parse a file by catting a file and piping the file output to a while read loop.
The following bash script produces it own code as output. Isn't it interesting?

code for dumping own file content

#!bin/bash

cat $0 | while read line
do
echo $line;
done

Secondly, using file descriptors we can play a lot with it. But, before we will learn about firl descriptors in brief.


Under unix/linux OS, files are referenced, copied, and moved by unique numbers known as file descriptors.

0 stdinIt is(devie) usually the keyboard or mouse from where the input comes.
1stdoutIt is where output is redirected e.g. screen or file
2stderrIt is where the standard error messages are routed by commands, programs, and scripts.
$ some_command 2>&1

this means, the command sends all of the error messages to the same output device that the standard output goes to, which is normally the terminal.

Now, we will use this file descriptors to write the same shell script we wrote before.( which outputs its own code)


#!bin/bash

exec 3<&0  #redirect standard input to file descritpor 3

exec 0<$0 # redirect current file content to standard input

while read LINE   # simply read from standard input now
do 
echo $LINE;
done

exec 0<&3  # revert back stanrd input from 3 to 0

Upgrading Ubuntu to Natty Narwhal

I recently upgraded Maverick Meerkat to Natty Narwhal. The new ubuntu is packed with Unity for default desktop session. The Unity brings a lot of improvements with it. Along with improved UI and perceivable improved speed we could see a new scrollbar which takes less space and looks nice. Real gift to Ubuntu lovers!! worth a try !!!

Removing packages from linux (ubuntu)

I tried to remove the packages from maverick (Ubuntu 10.10), today..and found some good ways to do it

(A)First search the package :

The traditional way:
dpkg -l | grep partial-name-of-package-to-be-removed
Using dpkg command ....more conspicuous way
dpkg -l '*partial-name-of-package-to-be-removed*'
For example, you want to remove jdk then
dpkg -l '*jdk*'

(B)There are two ways to remove packages:

Using apt command : ...the better way...

After getting the list of packages choose the appropriate one and run following command

* To remove packages:
sudo apt-get remove package-name
* To remove packages and dependencies:
sudo apt-get autoremove package-name
* To remove packages, dependencies and configuration files:
sudo apt-get purge package-name

Using dpkg command : ...the crude way...

* To remove packages:
sudo dpkg -r package-name

* To remove packages,and configuration files:
sudo dpkg --purge package-name
* To force removal, if got dependency error(this command turns errors into warning..):
sudo dpkg --purge --force-depends package-name

What is an Open System?

I used 'Open System' term many times but today I got some time to think, in detail, about the very deep meaning of it. So here it goes...

An open system is one for which the architecture is not a secret. Just take example of TCP/IP system, which is Open. The TCP/IP protocols are same for every system. Whether its your laptop-PC using TCP/IP or your android smart-phone downloading apps from market. The architecture is same irrespective of the systems that use them. So this has many advantages, UNIX is a very good example of Open system software.