4

With AJAX, when we perform a JSON request, we first have to pass the response received through eval:

var quote=eval("(" + xhr.responseText + ")");

Afterwards, to use some information from it, one has to do some old-school JavaScript:

document.getElementById("textarea").value=xhr.responseText;

...or to use a specific piece of information we use createTextNode like this:

// price is retrieved from PHP.
var text=document.createTextNode(price + ":" + quote.price);

Coming to jQuery, the same thing is as simple as this:

$.get("file.php",function(data){
    var text=data.price;
});

Why do I need to use AJAX at all when there are AJAX methods available in jQuery itself?

I have no idea about the advanced things as to what AJAX can and jQuery can't or vice-versa. What are AJAX and jQuery each good for and when should I use which?

icktoofay
  • 126,289
  • 21
  • 250
  • 231
HalfWebDev
  • 7,022
  • 12
  • 65
  • 103
  • 1
    You are comparing apples and oranges. – Martin York Dec 30 '11 at 07:11
  • check this out: http://stackoverflow.com/questions/3870086/difference-between-ajax-and-get-and-load – Sang Dec 30 '11 at 07:12
  • 1
    @LokiAstari... may be . this is why i brought this up . Just to make sure i am clear about not to compare apples and oranges in future. Please see if u can help me ! – HalfWebDev Dec 30 '11 at 07:14
  • 1
    @Sang...This explains and compares different ajax methods in JQUERY. And that certainly is not my question – HalfWebDev Dec 30 '11 at 07:16

3 Answers3

3

jQuery is a javascript library that makes writing javascript easy in terms of cross browser issues and provides several utilities method. Ajax is one of the techniques in javascript by which you access some server side code and manipulate your dom with the results you get from it. jQuery provides cross browser issues free wrapper methods in case of AJAX also to perform the same thing. So jQuery is just a helping library which helps you achieve several things in a easier manner one of which can be ajax too.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • @sushil....So if one is learning Jquery then there is no need to go to AJAX way ...is this what you trying to say ?? Or just suggest me which one should i choose ? – HalfWebDev Dec 30 '11 at 07:21
  • u need to understand the ajax principles. jQuery provides set of methods to help you write ajax in a easier way. Very much like if you are learning jQuery you should first know what javascript is and does. – sushil bharwani Dec 30 '11 at 07:24
  • ...wait when one learns jquery and its methods one automatically get to know what this will do for him . And i hardly learnt any javascript in detail before diving into jquery. so same goes with ajax i guess (correct me if i am wrong). i mean there are various AJAX methods available in Juery and if do know the basic thing that ajax does( just retrieves data underneath from server and manipulates DOM as we wish ) . What more will i need to know about ajax ? – HalfWebDev Dec 30 '11 at 07:41
  • @kushal yes you are right. if you know the basics of ajax then you can jump on to jQuery ajax. But then there are some ajax design principles that you would like to know even when when working with ajax jquery methods. So a book on ajax may not just contain basics but a few more things but yes knowing basics you can start using ajax. – sushil bharwani Dec 30 '11 at 07:53
  • 1
    for ajax there is one very good book but you need to have all the willingness in world to read that that is Ajax Design Patterns. For jQuery start you can read jQuery in Action. – sushil bharwani Dec 30 '11 at 12:17
  • @sushil...I know the Jquery basics . I was thinking of ordering Learning JQuery 3rd edition ! And which is the book for AJAX you are mentioning above ? – HalfWebDev Dec 30 '11 at 12:22
  • 1
    Ajax Design Patterns orielly Michael Mahemoff – sushil bharwani Dec 30 '11 at 12:28
  • @sushil...and suppose i have read the design patterns from the book you recommended .Then next book to read will be ? – HalfWebDev Dec 30 '11 at 12:33
  • i think then its sufficient knowledge for Ajax – sushil bharwani Dec 30 '11 at 12:39
0

Remember, jquery is ajax framework library. Ajax is a asynchronous communication mechanism which can be implemented using XMLHttpRequest (xhr) or jquery. jQuery is library supported by third party. One of these mechanisms is enough to implement Ajax functionality.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • @thinksteep...So if one is learning Jquery then there is no need to go to AJAX way ...is this what you trying to say ?? – HalfWebDev Dec 30 '11 at 07:20
  • 1
    Think ajax as , you may use land phone(xmlhttprequest) or cell (jquery) or satellite phone (someother framework). – kosa Dec 30 '11 at 07:39
  • @thinksteep...what an anology ! Now what will you recommend as best in most general scenarios or are satellite phones ALWAYS the best ones ! – HalfWebDev Dec 30 '11 at 07:46
  • You need describe the 'Best'. Best changes based on context. Let us say if your annual income is 12000$, you can't afford satellite phone. If your company prohibits from using third party libraries, you can't use jQuery, you have to live up XMLHttpRequest. As a general suggestion, I refer jQuery library, it has good features comparing with XMLHttpRequest. – kosa Dec 30 '11 at 15:32
  • Do companies really forbid third party libraries? or you just considering as example ? – HalfWebDev Dec 30 '11 at 16:19
  • They do for whole lot of reasons (security, licensing and so on..) – kosa Dec 30 '11 at 16:54
0

jquery's ajax method is a great and powerful it can handle everything as you need.

in jquery there are various methods available to perform AJAX calls according to your requirements, but all of these are synonyms of ajax method of jquery.

for the json you can use $.getJSON(url,[data],function(response){});

for posting data you can use `$.post(url,[data],function(response){});

for get request you can use `$.get(url,[data],function(){});

and if you want to use ajax method for all of these things you have to pass various arguments according to your requirement

$.ajax({
'url':you url,
'type':request type,
'data':your data,
'success':success handler function,
'error':error handler function,
/*and many more*/
)}

read jquery documentation for full details

Dau
  • 8,578
  • 4
  • 23
  • 48