Jun 18, 2009

Root Login in Ubuntu

In this post I am going to explain how to enable root login from the Login window in Ubuntu.

Ubuntu , by default, does not allow the root user to login from the login window

To change this behaviour follow these steps.

1.Login as a normal user

2.Open System -> Administration -> Login Window
It will ask for the password, enter your password

3.In the Security Tab, under the Security section select
Allow local system administrator login
Close it.

4.Open System -> Administration -> Users and Groups

5.Here root is disabled. Select the root and click Unlock button.
Again enter your password.

6.Then click on Properties button.
If properties is disabled , clicking root again will make it active.

7.In the newly opened 'Account root Properties window', you can set the root password
under the Password section

8.Click OK and then logout and try login as root.

Edit(07-12-2009): In Ubuntu9.10 you can just type
sudo passwd root
in the terminal. It will ask for the password and confirm password.
Afterwards the root user will be enabled in the login screen

Feb 18, 2009

AJAX - Asynchronous JavaScript and XML

Ajax stands for Asynchronous JavaScript and XML.
It is used for updating the contents of a page with data from the server or for making requests to the server without refreshing or submitting page to server. This is implemented using the XMLHttpRequest. XMLHttpRequest is an implementation of the ActiveX object, called XMLHTTP.

Anatomy of XMLHttpRequest Object
XMLHttpRequest has 6 properties and 6 Methods(at least that's what i know) as listed below.
Properties
1.status: gives the HTTP status code from the server such as 200, 404 ,500
2.statusText: gives the text associated with HTTP status code such as OK for 200 and Page Not Found for 404
3.responseText: the response from the server as a string.
4.responseXML: the response from the server as an XML document.We have to use DOM manipulation methods to process the response.
5.readyState: defines the state of the XmlHttpRequest.
possible values is as follows:
* 0 (uninitialized)
* 1 (loading)
* 2 (loaded)
* 3 (interactive)
* 4 (complete)
6.onreadystatechange:defines the method to be called on every state change of the request object(same as an event handler for the asynchronous callback).

Methods
The methods are self explanatory.
1.abort()
2.getAllResponseHeaders()
3.getResponseHeader(header)
4.setRequestHeader(header, value)
5.open(method, url, isAsynchronous): prepares our object to send the request to server.
method- is any HTTP request method ( GET, POST, HEAD or any other method )
url- is the address of the page we are requesting. we can not make cross domain calls. can be relative or absolute.
isAsynchronous- value "true" for this param sets the request as asynchronous.
6.send(body):sends the request. body param is used to send parameters as name-value pair. It will be null for GET method as we have already appended the parameters to url. For POST, body should be in the form of a query like "name1=value1&name2=value2"

Let's Get to the Business
So that's about our XMLHttpRequest object. Now let's write some code to make of use this object. First we will create a variable of XMLHttpRequest. 2. Prepare it for sending the request and then send it. 3.Handle the response from the server.

Create XMLHttpRequest object
To create a XMLHttpRequest object that works in all major browsers, we need to use the following code, thanks to zillion vendors and versions.

try{
// Opera, Firefox, Safari,IE7
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Older Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your Browser does not support XMLHttpRequest.");
return false;
}
}
}


Send the Request
We have to set some properties of this object to prepare it for sending the request.
ajaxRequest.onreadystatechange = processResponse;
This sets the method which should be called when the response arrives. Or we can write an inline function to process the response like,
eg.1.ajaxRequest.onreadystatechange = function() { alert(httpRequest.responseText); };
OR
eg.2.ajaxRequest.onreadystatechange = function() { anotherFunction(ajaxRequest); };

If the variable ajaxRequest is global one, successive invocation of the function may overwrite each other. So it is better to create a local ajaxRequest variable and pass it to another function for processing as shown in eg.2.

For sending the request,first call the open() method and then send().
ajaxRequest.open("GET", url+params, true);
ajaxRequest.send(null);

If the method is POST,
we have to set the Content-Type to "application/x-www-form-urlencoded" and the parameters will be passed with the send() method. So the code will be
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(params);

Handle the Response
Now the request is sent. Whenever the state of the request object changes, the method
processResponse will be invoked. So in our processResponse function we will check if the readyState of the request object is 4 or not. If it is 4 , that means the response is complete and we can process it. Now we have to check the status code of the response. If it is 200 , we are all set. We can get the response using responseText or responseXML.

if (ajaxRequest.readyState == 4) { // Complete
if (ajaxRequest.status == 200) // OK response
document.getElementById("result").innerHTML =
ajaxRequest.responseText;
else
alert("Error in retrieving data from Server."+
ajaxRequest.statusText );
}

If we are using responseXML , it should be treated as a XML document object and use DOM methods to traverse the XML document.


Combine the Snippets


var ajaxRequest; // The XMLHttpRequest variable.
function sendRequest(url,method) {
try{
// Opera, Firefox, Safari,IE7
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Older Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e){
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your Browser does not support XMLHttpRequest.");
return false;
}
}
}
ajaxRequest.onreadystatechange = processResponse;
var url="test.html";
var params="name1=value1&name2=value2";

if(method=="POST"){
ajaxRequest.open("POST", url, true);
ajaxRequest.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length",
params.length);
ajaxRequest.send(params);
}
else if(method=="GET"){
ajaxRequest.open("GET", url+'?'+params, true);
ajaxRequest.send(null);
}
}

function processResponse() {
try {
if (ajaxRequest.readyState == 4) { // Complete
if (ajaxRequest.status == 200) { // OK response
document.getElementById("result").innerHTML =
ajaxRequest.responseText;
//"result" is the id given for a div element in the html.
} else {
alert("Error in retrieving data from Server."+
ajaxRequest.statusText );
}
}
}
catch( e ) {
alert('Caught Exception: ' + e.description);
}
}

May be helpful:
  1. List of HTTP Status Codes. go there
  2. List of HTTP Methods. let me see
  3. Read about the origin policy here.
  4. If u r desperate to make a cross domain call, read about "UniversalBrowserRead".
  5. If 4 didn't work check this article. OK
  6. Some ready made ajax solutions. get it