- 10 Jaw Dropping HTML5 and Javascript Effects http://bit.ly/auBD9g... 1 day ago
- 30 Clean and Simple Free Fonts http://bit.ly/bl9TcU... 3 days ago
- Follow me
AJAX is a wonderful piece of technique that has changed the way website response to user input. I like AJAX a lot, I discovered the goodness of AJAX 2 years ago, but the development was quite hard without jQuery framework. As a result, I didn't really keen to use it back then. Now, I'm with jQuery, and jQuery has made it's so much easier to integrate AJAX into a website.
I assume you all have experiences with AJAX. As a AJAX lover, I found it can be quite hard to debug, especially debugging other developers code. It's because of too many technologies are involved in this technique - CSS, Javascript and PHP. So, I created this article and to share what I have learnt to all of you.
AJAX is abbrieviated from Asynchrounous javascript and XML. It's not a new technology, but the implementation of a group of technologies to achieve a seamless interaction between client and server.
Typically, xhtml and css present the information to user and javascript is used to handle user interactions, and a server side language is to perform users' requests and might return result back to the user. It's all happening in the background using the Javascript XMLHttpRequest. Javascript plays a main role to tie all these technologies together and create the asynchronous interaction between client ans server.
Advantages:
Usability Guidelines:
A lot of times, it's the server script (php, asp.....) that causing the problems. I'm a PHP developer, everytime before I plug it into the AJAX, I would do some testing by passing parameter in the URL first to make sure it generate desired result.
We will not go through too much of the debugging skills because the best way to learn it is to figure it out by yourself, however I will introduce some of the basic tools and codes that could help you to reduce human errors and syntax errors.
//Sample data - array
$data = array('apple', 'orange', 'durian');
var_dump($data);
//or
print_r($data);
//but normally I would create a function like this:
function dump($data) {
echo '<pre style="background:#fff; text-align:left">';
var_dump($data);
echo'</pre>';
}
//Assume this AJAX script test.php will call by Javascript using POST method, //but it will be easier to test it with GET method. $name = ($_POST['name'] ? $_POST['name'] : $_GET['name']); $email = ($_POST['name'] ? $_POST['name'] : $_GET['name']); echo $name; echo $mail;
Refer the code above, we will able to pass parameters by using the following URL:
http://www.someurl.com/test.php?name=Kevin@email=Kevin@someurl.comYep, with just simple switching, you will able to test the functionality of your server side script.
This one is a bit tricky, because I found that different browsers have different way to read javascript. For example, firefox is more forgiveful, if you forget to put semicolon in some lines, the script will still work as long as it's on separate line. But for Internet explorer, that's not the case. It will display an annoying yellow icon on the status bar.
However, I do have some tools and tips on my hand to assist me:
Make sure you're passing the right parameters to the URL. You can check it using alert() to display the data or declare an ID called #debug and print it out using jQuery $(). The main purpose is to print out the output
Consider the following scenario, if your AJAX doesn't work, you can always check and make sure the data is correct by using this method:
var param = 'name=' . $('input#name').val() . '&email=' + $('input#email').val();
//use this to ensure data is well-formed
alert(param);
//or assume you have <p id="debug"> declared in the html doc
$('#debug').html(param);
$.ajax({
type: "GET",
url: "test.php",
data: param,
cache: false,
success: function (html) {
$('#result').append(html);
}
});
A lot of times, I missed the ampersand or put the ampersand in an incorrect position.
Two common mistakes:
//forgotten to put question mark http://www.somedomain.com/save.php&name=kevin&email=test@test.com //forgotten to put ampersand in between name's data and email parameter http://www.somedomain.com/save.php?&name=kevinemail=test@test.com
var param = 'name=' . $('input#name').val() . '&email=' + $('input#email').val();
$.ajax({
type: "GET",
url: "test.php",
data: param,
cache: false,
success: function (html) {
alert(html);
}
});
Debugging code is a time consuming process and it's a good practice to keep on testing it from the beginning. You do not want to write large amount of code and test it later. It will only make the debugging process harder. I hope my techniques will able to help you, and if you have any better ways to test it, do let me know :). Thanks.
The effects and techniques demonstrated in tutorials on Queness can be used in whatever manner you wish without attribution. You cannot copy whole tutorials (unless permission is given), either in English or translated to another language.
Buy wholesale computers directly from China at DHgate.com
Discover the tools to build your e-Commerce Site with Netfirms
Buy China Products from Made-in-China.com
Cocktail Dresses
SmartPhone Cell Phone
Wholesale electronics
Web Hosting Rating
Buy WOW Gold
Get your cdl today
Debt collector review
Recent Comments