Mastering ajax data loading - ajax

It's not that I'm not familiar with the concept but I'm wondering about what is the best approach when you are creating applications supporting ajax userexperience.
I mostly use ajax with jQuery but also when I want to load information without a pagerefresh. As you will probably know, the XmlHttp Object and the ResponseText provides a nice and easy way to execute a script in the back and display the results e.g. a div.
A bit of a downside of this approach is that its hard to see the actual generated sourcecode. I often take a look at the sourcecode to see if the expected parameters are correctly provided to e.g. formelements.
So I'm curious, what is your approach for creating ajax-data-load functionality? Is it just by the ResponseText property?

Having manually crafted XmlHttpRequest objects for a while, I now use jQuery for all my AJAX-ified stuff. It gives you much better control while coding less.
If you use Firefox, get yourself the Web Developer Toolbar. It has a cool feature called "view generated source code" which generates the HTML code that the browser knows about in the current document as it stands, so it includes the HTML sent back by your AJAX requests.
Also I make it a rule to always tell the user you're loading something rather than relying on the browser to tell them (like Gmail's "Loading" text in the corner for instance.)

Related

How do we do AJAX programming

I have no idea about AJAX programming features. I just know that it is Asynchronous Javascript and XML.
Please help me in knowing about this language.
I have gone through many AJAX tutorials. But none of the programs are running. Why I don't know.
Do we save the file with .HTML extension?
Read:
AJAX Tutorial by W3Schools.
AJAX Programming by Google Code University
To start coding you can get the Ajax Control Toolkit by Microsoft. You should read Ajax Control Toolkit Tutorials to get a grasp of it.
You can use the free Microsoft Visual Web Developer 2010 Express Edition as your IDE.
Aside from the correct responses that the others gave you, judging from your question I think you first need to learn about client-side and server-side code.
Do we save the file with .HTML extension?
Yes and no. You will have an HTML frontend, that for instance contains a button. This will be interpreted from the client's (=user) browser. In fact it may be rendered differently depending on the browser/OS/etc.
Now, you attach some Javascript code to this button. This also runs on the client's browser, and creates a XMLHttpRequest object, either directly or through the use of a library (JQuery & Co.). Note that a library is not necessary to do an AJAX request. It will make your life easier if you do a lot of AJAX calls, but it is not essential.
And here's where the magic happens: the XMLHttpRequest object will call asynchronously (i.e.: without reloading the page) a server-side page. This may be a PHP, ASP, Perl etc etc file that does something on the server, for instance queries a database. This part of the operation is absolutely independent from the client. The user can close the browser before the server-side code finishes to load and the server will not know about it.
Once the server-side code has finished executing it returns to the client with some response data (e.g. a piece of XML, JSON, HTML or whatever you like). Finally the client executes (or not) some other Javascript code in response to this, for example to write on the screen, again with no reloading of the page, something based on what the server has returned.
Maybe I can help you understand AJAX by clarifying the concepts a bit.
Please help me in knowing about this language.
AJAX is not a language, it is a way of using existing techniques to improve the user experience of a web site. The language is Javascript in the browser but you can use any server side technique that you feel comfortable with (ASP.NET, Java, PHP, Ruby etc.)
Do we save the file with .HTML extension?
Well, that is not really the point. What you have to grasp here is that there is a server and a browser that interact with each other. Yes, you can use static HTML files for your pages (and save them as .html files), but you'll need a server to respond to the requests of the browser. This may be why your sample code is not working; you need to set up a server that works with your pages.
The whole idea behind AJAX is to improve the user experience by not reloading the entire page when a user interacts with it. You request the data you need and update the page by using Javascript to update the HTML. This is called an out-of-band or asynchronous request.
I just know that it is Asynchronous Javascript and XML.
That is what the acronym stands for but it doesn't quite cover what the technique is for, nor is it accurate any more. In the beginning XML was used to transfer data from the server to the client. People found that XML is not really that easy to work with in Javascript so now it's more common to use JSON. JSON is a snippet of javascript that can be evaluated in the browser. The snippet creates javascript object(s) that represent the data.
If you use a Javascript library, like others have suggested here, you won't have to worry about many of the details though.
Before you get into AJAX you should make sure that you understand:
HTML and CSS
Javascript
how to modify HTML with Javascript
how a browser requests information from a server
how to handle requests on the server
If you are not comfortable with all of these concepts, stick with 'regular' web pages and try to improve your knowledge step by step.
Once you get the basic knowledge from W3school, I suggest you use a framework. Usually developers do not use XMLHttpRequest at all. Instead, javascript frameworks like ExtJS, jQuery and other frameworks make your work simple. I suggest you learn bit of javascript as well. check out jQuery.
Just to add that AJAX is rarely used in its pure form with XMLHttpRequest. You will often use it as a part of AJAX UI libraries which make your life easier. If you are from the Java world - such an AJAX library is Richfaces.
Instead of worrying about how to do AJAX, use something that allows you to forget about it. Frameworks like NOLOH do AJAX (and Comet) for you automatically without you having to do a thing. Just concentrate on your application, and business logic and it does the rest.
Really, everything is done via AJAX if available, automatically. No work on your part. If you're don't want to spend much time researching it, check out this short video that was demonstrated at Confoo PHP Conference this past March http://www.youtube.com/phpframework#p/u/11/cdD9hSuq7aw.
For all those worried about, well, if it's all AJAX, what about search engines? No need to worry, http://dev.noloh.com/#/articles/Search-Engine-Friendly/.
So instead of having to worry about all these different technologies, or the client-server relationship, you can sit down, code and have your website/WebApp working in no time.
You can read about NOLOH is this month's cover story of php|architect magazine, http://www.phparch.com/magazine/2010/may/.
Enjoy.
Disclaimer: I'm a co-founder of NOLOH.
It is easy one. Ajax getting data from server side by client side execution. We have to do use XMLHttpRequest to get the result.

Ajax architecture in Django application

I am trying to find the optimal architecture for an ajax-heavy Django application I'm currently building. I'd like to keep a consistent way of doing forms, validation, fetching data, JSON message format but find it exceedingly hard to find a solution that can be used consistently.
Can someone point me in the right direction or share their view on best practice?
I make everything as normal views which display normally in the browser. That includes all the replies to AJAX requests (sub pages).
When I want to make bits of the site more dynamic I then use jQuery to do the AJAX, or in this case AJAH and just load the contents of one of the divs in the sub page into the requesting page.
This technique works really well - it is very easy to debug the sub pages as they are just normal pages, and jQuery makes your life very easy using these as part of an AJA[XH]ed page.
For all the answers to this, I can't believe no one's mentioned django-piston yet. It's mainly promoted for use in building REST APIs, but it can output JSON (which jQuery, among others, can consume) and works just like views in that you can do anything with a request, making it a great option for implementing AJAX interactions (or AJAJ [JSON], AJAH, etc whatever). It also supports form validation.
I can't think of any standard way to insert ajax into a Django application, but you can have a look to this tutorial.
You will also find more details on django's page about Ajax
Two weeks ago I made a write up how I implement sub-templates to use them in "normal" and "ajax" request (for Django it is the same). Maybe it is helpful for you.
+1 to Nick for pages displaying normally in the browser. That seems to be the best starting point.
The problem with the simplest AJAX approaches, such as Nick and vikingosegundo propose, is that you'll have to rely on the innerHTML property in your Javascript. This is the only way to dump the new HTML sent in the JSON. Some would consider this a Bad Thing.
Unfortunately I'm not aware of a standard way to replicate the display of forms using Javascript that matches the Django rendering. My approach (that I'm still working on) is to subclass the Django Form class so it outputs bits of Javascript along with the HTML from as_p() etc. These then replicate the form my manipulating the DOM.
From experience I know that managing an application where you generate the HTML on the server side and just "insert" it into your pages, becomes a nightmare. It is also impossible to test using the Django test framework. If you're using Selenium or a similar tool, it's ok, but you need to wait for the ajax request to go return so you need tons of sleeps in your test script, which may slow down your test suite.
If the purpose of using the Ajax technique is to create a good user interface, I would recommend going all in, like the GMail interface, and doing everything in the browser with JavaScript. I have written several apps like this using nothing but jQuery, state machines for managing UI state and JSON with ReST on the backend. Django, IMHO, is a perfect match for the backend in this case. There are even third party software for generating a ReST-interface to your models, which I've never used myself, but as far as I know they are great at the simple things, but you of course still need to do your own business logic.
With this approach, you do run into the problem of duplicating code in the JS and in your backend, such as form handling, validation, etc. I have been thinking about solving this with generating structured information about the forms and validation logic which I can use in JS. This could be compiled at deploy-time and be loaded as any other JS file.
Also, avoid XML. The browsers are slow at parsing it, it is a pain to generate and a pain to work with in the browser. Use JSON.
Im currently testing:
jQuery & backbone.js client-side
django-piston (intermediate layer)
Will write later my findings on my blog http://blog.sserrano.com

What is AJAX, really?

I have to start using AJAX in a project and I don't know where to start. Can someone please help?
Asynchronous JavaScript And Xml. A technique for achieving bi-directional, script-driven communications between Web browsers and servers via HTTP.
See also:
definition on Wikipedia
AJAX Introduction on w3schools
Ajax Workshop 1 on Ajax Lessons
Edit: As pointed out by Nosredna, JSON is often used in place of XML.
The rough idea in English:
You have a web page. Some event (can be a button press or other form event, or just something triggered by a timer) occurs and triggers JavaScript code that asks the server for fresh information (like the latest value of GOOG stock).
There's a piece of code on the server that collects the info you passed and sends some info back. That's different from the page-serving job the server usually has.
When the server answers, a callback function (that you specified in the JavaScript call to the server) is called with the info from the server. Your JavaScript code uses the info to update something--like a GOOG stock chart.
Not to be confused with the cleaner, AJAX, the technology term, is really describing a framework or better stated as a technique for using XML and JavaScript to make asynchronous calls to server side code...
Here are some good code samples. And some more.
While many of these samples above show how to create all of the XML Request objects, if you look into the AJAX Control Toolkit from Microsoft for ASP.NET applications or jQuery, you'll find these easier to work with.
jQuery Sample (from jQuery site):
when code is hit, the some.php file is hit passing the name and location values in.
<script type="javascript">
function saveDataAjax(){
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
}
</script>
<input type="submit" onClick="saveDataAjax();" value="submit" />
It's a buzzword, the essence of it is:
Using Javascript to make an asynchronous HTTP request (in the background).
When the content arrives, an action is taken, usually performing some logic then updating the appearance of the page by manipulating the DOM tree; meaning, inserting new HTML elements, deleting some html elements, etc.
The X in AJAX stands for XML, but it's irrelevant. XML is just one of many ways to format the data that's sent by the server. JSON is a much better alternative (IMNSHO). Also, the server can send plain text or just regular html.
The keyword here is asynchronous request. A request that happens in the background, without the browser having to reload the page.
From the Pragmatic Ajax book:
What Is Ajax?
Ajax is a hard beast to distill into a
one-liner. The reason it is so hard is
because it has two sides to it:
Ajax can be viewed as a set of
technologies.
Ajax can be viewed
as an architecture.
Ajax: Asynchronous JavaScript and
XML
The name Ajax came from the bundling
of its enabling technologies: an
asynchronous communication channel
between the browser and server,
JavaScript, and XML. When it was
defined, it was envisioned as the
following:
Standards-based presentation using XHTML and CSS
Dynamic display and interaction using the browser’s DocumentObject
Model (DOM)
Data interchange and manipulation using XML and XSLT
Asynchronous data retrieval using XMLHttpRequest or XMLHTTP (from
Microsoft)
JavaScript binding everything together
Although it is common to develop using
these enabling technologies, it can
quickly become more trouble than
reward.
It is for these reasons that the more
important definition for Ajax is...
Ajax: The Architecture
The exciting evolution that is Ajax is
in how you architect web applications.
Let’s look first at the conventional
web architecture:
Define a page for every event in the application: view items, purchase
items, check out, and so on.
Each event, or action, returns a full page back to the browser.
That page is rendered to the user.
This seems natural to us now. It made
sense at the beginning of the Web, as
the Web wasn’t really about
applications. The Web started off as
more of a document repository; it was
a world in which you could simply link
between documents in an ad hoc way. It
was about document and data sharing,
not interactivity in any meaningful
sense.
Picture a rich desktop application for
a moment. Imagine what you would think
if, on every click, all of the
components on the application screen
redrew from scratch. Seems a little
nuts, doesn’t it? On the Web, that was
the world we inhabited until Ajax came
along.
Ajax is a new architecture. The
important parts of this architecture
are:
Small server-side events: Now components in a web application can
make small requests back to a server,
get some information, and tweak the
page that is viewed by changing the
DOM. No full page refresh.
Asynchronous: Requests posted back to the server don’t cause the
browser to block. The user can
continue to use other parts of the
application, and the UI can be updated
to alert the user that a request is
taking place.
onAnything: We can interact with the server based on almost anything
the user does. Modern browsers trap
most of the same user events as the
operating system: mouseovers, mouse
clicks, keypresses, etc. Any user
event can cause an asynchronous
request.
This all sounds great, doesn’t it?
With this change we have to be
careful, though. One of the greatest
things about the Web is that anybody
can use it. Having simple semantics
helps that happen. If we go overboard,
we might begin surprising the users
with new UI abstractions. This is a
common complaint with Flash UIs, where
users are confronted with new symbols,
metaphors, and required actions to
achieve useful results.
Most commonly, it refers to the use of the XMLHttpRequest object via JavaScript* in a browser.
Depending on who you ask, it could be used to describe almost any type of client/server communication over HTTP other than just typing a URL into a browser.
*jQuery provides some nice wrapper code to handle cross-browser differences, etc.
Ajax is a bit of a misnomer. To quote the wiki article:
Despite the name, the use of
JavaScript and XML is not actually
required, nor do the requests need to
be asynchronous.
Whereas now most people call "ajax" any type of
web application that communicates
with a server in the background
http://www.w3schools.com/Ajax/Default.Asp
that is a good place to start. This should answer all of your questions.
From the man that coined the term - http://adaptivepath.com/ideas/essays/archives/000385.php
"Ajax" is the successfull marketing term introduced back in 2005 to replace the the older term "DHTML" that did not stick well. "Ajax" today is part of the history too as the new word - "HTML5" emerge. Still "HTML5" is pretty much what original "DHTML" used to be.
Ajax is also reffered to as "the new approach to the application development" where a web page is created on the server initially but later on, during its lifetime, the updates are being done on the client as the data or partial content gets communicated to the server in a background.
Hope this clarifies.
Just to add.. may be not relevant for the question ..
although, AJAX was made famous by Gmail in their browser emails ..the credit of AJAX goes to Microsoft .. they created the AJAX thing..
I believe the fastest and easiest way to get started is with jQuery:
http://jquery.com/
http://docs.jquery.com/Ajax/jQuery.ajax#examples
AJAX stands for asynchronous JavaScript and XML, though it doesn't always deal with XML data anymore. Essentially it boils down to using the XMLHttpRequest object through JavaScript running on the client to make a web request and retrieve some information that you use to update the state of your page without requiring a page refresh.
Start with a basic tutorial that shows you how to use bare bones Ajax to make asynchronous requests such as http://www.w3schools.com/Ajax/Default.asp before moving on to using it in a production level application.
When using it in an application you're far better off investigating one of the common JavaScript frameworks that abstract away the differences between the various browsers and make it easy to manipulate the page after the request returns. I personally recommend http://www.jquery.com/
I read Head First AJAX as my first AJAX reference and I found it to give a simple and practical overview of AJAX.
Creative use of previously known technology. Both the browser side scripting and programmatic access to data on the server have been known before. In AJAX it has been put together for innovative use anabling new applications of thechology known before. The REST comes to mind as similar type of advance...
AJAX is very simple : someone somewhere tought that it would be cool to be able to send something to the server and receive something from it without reload a page.
AJAX is not a revolution, it's just a name for something simple : a web page can send a request to the server without being reloader - just some asynch stuff here.
You can add AJAX controls on your web pages wihout any works - just drag them in with Visual Studio. You may have to add some manager for them, but it is simply a drag-and-drop task.
But be warned : rogue web browser usually don't speak the same AJAX language as IE...
:)
AJAX is really fancy term for giving the browser the ability to refresh parts of its content with the need to reload an entire page. Like many have said, it doesn't require XML, or even Javascript in order to implement it. In fact in its early days it was done with with VBScript and Jscript and just called DHTML. Jesse James Garrett may have invented the AJAX term, but it was really Microsoft that invented the concept behind it.
This source says Microsoft started it in 1999, but I would date the birth of this technology even further. This Wired article is probably more accurate on the date of this technology being in the late 90's, much of it coming from the old days of the MSDN DHTML Dude columns written by Michael Wallent at Microsoft which started back in 1997. Much of the story is also told in this great video here by Michael himself: http://channel9.msdn.com/posts/Charles/Michael-Wallent-Advent-and-Evolution-of-WPF/ Megan still works at Microsoft by the way working on the Silverlight team nowadays, Microsoft's replacement for ActiveX.
Back to the AJAX thingy...when Jesse James Garrett back in 2005 he was mostly talking about the use of XMLHTTPRequest within Javascript code, and a dash of salt. That later began a hip word that many people started using even though they had no idea what it was, and thought that is really something brand new and hip, when really it was just a remix of something old.....sort of like many hip-hop songs you hear nowadays.
It's not new, just a newer version of something old!
I'll give it a try and say that "it's the concept of having a W3C based (JavaScript, HTML and CSS) solution for building Rich Applications for running on the web in a browser"
Everything else is just "technical details" I guess ... ;)
PS! - AMAZING question ...!! ;)
AJAX (Asynchronous JavaScript and XML) is a newly coined term for two powerful browser features that have been around for years, but were overlooked by many web developers until recently when applications such as Gmail, Google Suggest, and Google Maps hit the streets.
To know more information about Ajax learn Ajax tutorial
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.(FROM w3school). to understand simply: when we request for a link or submit form we request a synchronously to server for data. webpage destroy current page and regenerate new page. but with AJAX browser can send the same request without repainting the entire page.
It's JavaScript, but it works.

Screen scraping an ASP.NET web page to retrieve data displayed in the grid view

I am using RUBY to screen scrap a web page (created in asp.net) which uses gridview to display data. I am successfully able to read the data displayed on page-1 of the grid but unable to figure out how I can move to the next page in the grid to read all the data.
Problem is the page number hyperlinks are not normal hyperlinks (with URL) but instead are javascript hyperlink which causes postback to the same page..
An example of the hyperlink:-
6
I recommend using Watir, a ruby library designed for browser testing, if you're already using ruby for processing. For one thing, it gives you a much nicer interface to the DOM elements on the page, and it makes clicking links like this easier:
ie.link(:text, '6').click
Then, of course you have easier methods for navigating the table as well. It's easy enough to automate this process:
1..total_number_of_pages.each do |next_page|
ie.link(:text, next_page).click
# table processing goes here
end
I don't know your use case, but this approach has its advantages and disadvantages. For one thing, it actually runs a browser instance, so if this is something you need to frequently run quietly in the background in completely automated way, this may not be the best approach. On the other hand, if it's ok to launch a browser instance, then you don't have to worry about all that postback nonsense, and you can just click the link as if you were a user.
Watir: http://wtr.rubyforge.org/
You'll need to figure out the actual URL.
Option 1a: Open the page in a browser with good developer support (e.g. firefox with the web development tools) and look through the source to find where _doPostBack is defined. Figure out what URL it's constructing. Note that it might not be in the main page source, but instead in something that the page loads.
Option 1b: Ditto, but have ruby do it. If you're fetching the page with Net:HTTP you've got the tools to find the definition of __doPostBack already (the body as a string, ruby's grep, and the ability to request additional files, such as those in script tags).
Option 2: Monitor the traffic between a browser and the page (e.g. with a logging proxy) to find out what the URL is.
Option 3: Ask the owner of the web page.
Option 4: Guess. This may not be as bad as it sounds (e.g. if the original URL ends with "...?page=1" or something) but in general this is the least likely to work.
Edit (in response to your comment on the other question):
Assuming you're using the Net:HTTP library, you can do a postback by just replacing your get with a post, e.g. my_http.post(my_url) instead of my_http.get(my_url)
Edit (in response to danieltalsky's answer):
watir may be a really good solution for you (I'm kicking myself for not having thought of it), but be aware that you may have to manually fire the event or go through other hoops to get what you want. As a specific gotcha, with any asynchronous fetch like this you need to make sure that the full response has come back before you scrape it; that isn't a problem when you're doing the request inline yourself.
You will have to perform the postback. The data is pass with a form POST back to the server. Like Markus said use something like FireBug or the Developer Tools in IE 8 and fiddler to watch the traffic. But honestly this is a web form using the bloated GridView and you will be in for a fun adventure. ;)
You'll need to do some investigation in order to figure out what HTTP request the javascript execution is performing. I've used the Mozilla browser with the Firebug plugin and also the "Live HTTP Headers" plugin to help determine what is going on. It will likely become clear to you which requests you will need to make in order to traverse to the next page. Make sure you pay attention to any cookies getting set.
I've had really good success using Mechanize for scraping. It wraps all of the HTTP communication, html parsing and searching(using Nokogiri), redirection, and holding onto cookies. But it doesn't know how to execute Javascript, which is why you will need to figure out what http request to perform on your own.

How is AJAX implemented, and how does it help web dev?

From http://en.wikipedia.org/wiki/AJAX, I get a fairly good grasp of what AJAX is. However, it looks like in order to learn it, I'd have to delve into multiple technologies at the same time to get any benefit out of it. So two questions:
What are resources that can help me understand/use AJAX?
What sort of website would benefit from AJAX?
If you aren't interested in the nitty gritty, you could use a higher-level library like JQuery or Prototype to create the underlying Javascript for you. The main benefit is a vastly more responsive user interface for web-based applications.
There are many libraries out there that can help you get benefit out of AJAX without learning about implementing callbacks, etc.
Are you using .NET? Look at http://ajax.asp.net. If you're not, then take a look at tools like qcodo for PHP, and learn about prototype.js, jquery, etc.
As far as websites that would benefit: Every web application ever. :) Anything you interact with by exchanging information, not just by clicking a link and reading an article.
Every website can benefit from AJAX, but in my opinion the biggest benefit to AJAX comes in data entry sections - forms basically. I have done entire sites where the front end - the part the user sees had almost no AJAX functionality in it. All the AJAX stuff was in the administration control panel for assisting in (correct!) data entry.
There is nothing worse than submitting a form and getting back an error, using AJAX you can pretty much prevent this for everything but file uploads.
I find it easiest to just stay away from all the frameworks and other helpers and just do basic Javascript. This not only lets you understand what's going on under the covers, it also lets you do it in the simplest way possible. There's really not much to it. User the JS XML DOM objects to create an xml document client side. Sent it to the server with XMLHTTPRequest, and then process the result, again using the JS XML DOM objects. Start with something simple. Just try sending one piece of information to the server, and getting a small piece of information back.
The Mozilla documentation is good. Sites that benefit from it the most are ones that behave almost like a desktop application and need high interactivity. You can usually improve usability on almost any site by using it, however.
Ajax should be thought of as a means to alter some content on a page without reloading the entire page.
So when do you need to do this? Really only when you have some user interactions or form information that you want to keep intact while you change some content on the page.

Resources