2

What are the pros/cons and differences between running AJAX code through JQuery and running it through XMLHttpRequest? I assume it's the same since JQuery is just a Javascript library, but again there must be something more...

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
iTakeHome
  • 189
  • 1
  • 1
  • 11
  • 5
    jQuery is javascript and just wraps native methods and adds additional utilty. What is the context of this question? – charlietfl Dec 25 '14 at 13:00
  • The differences will mostly be with the conveniences that jQuery adds around the native `XMLHttpRequest` API, like formatting request data, parsing the response, and mixing in ` – Jonathan Lonowski Dec 25 '14 at 13:12
  • There isn't a significant benefit to a pure JavaScript approach that outweighs the numerous advantages already mentioned for a jquery approach. Unlike most tasks where implementing jquery for a one-time-use case might be overkill, I would advise using jquery if you need even one ajax request in your script. – Anthony Dec 25 '14 at 13:27

3 Answers3

1

You can do Everything including AJAX in jQuery and Javascript.

But reasons to prefer jQuery (Demerits of Javascript):

  1. No Cross browser support.

  2. Complex coding

  3. More coding for simple functionality

  4. You cant waste time in testing in all browsers+their versions

  5. There is Great documentation on jquery site but javascript lacks it.

  6. All Great companies use it -> https://jquery.org/members/

  7. What is easy for you ?

Javascript

function loadXMLDoc() {
  var xmlhttp;
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("POST","demo_post2.asp",true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("fname=Henry&lname=Ford");
}

Or

jQuery

$("button").click(function(){
  $.ajax({url:"demo_test.txt",success:function(result){
    $("#div1").html(result);
  }});
});

Bottomline -> jQuery is a layer on javascript used to simplify in better way what you could (and could not) do with javascript.

So rely on jQuery for ajax call.

Jozef Dúc
  • 965
  • 2
  • 18
  • 29
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • 1) If JS had no cross-browser support, neither would jQuery... 2) personal opinion, depends on skills... 3) not always true + the burden of heavy and slow library... 4) ?... 5) There's a lot more documentation on JS, one of them: https://developer.mozilla.org/en/docs/Web/JavaScript... 6) Even greater companies use plain JS: http://vanilla-js.com/... – Shomz Dec 25 '14 at 13:27
  • @shomz - you are wrong. – Anthony Dec 25 '14 at 13:29
  • 1
    I listed 6 things... and you say I'm wrong... is that a jQuery-way of argumenting? – Shomz Dec 25 '14 at 13:31
  • 1
    this isn't really an answer it's just an opininon – charlietfl Dec 25 '14 at 13:32
0

I think like anything jQuery; it's simply a question of a convenience method to simplify and/or encourage x-browser support

ne1410s
  • 6,864
  • 6
  • 55
  • 61
0

Yes, you are right, it's the same since jQuery is just a JS library...

There's not a single thing you can do in jQuery that you can't do in plain JS. And if the performance really matters (CPU-intense app, game, etc.), you'd stay away from such a heavy library. See this: http://vanilla-js.com/ You'll find a lot of interesting performance and code comparisons there (biased towards JS, I agree, but I think it's for a reason).

The only benefit from using AJAX using jQuery is having to write a little less code, again for a price of having a slow library. For a simple website, you won't notice the performance difference, but for anything more demanding... let's say I know a lot of people (including myself) that end up rewriting jQuery functionality to plain JS. Again, it all depends on what you want to do with it - if you only need the AJAX functionality, I'd strongly suggest writing a wrapper function in plain JS and going with it; but if you don't want to bother with all that and feel more comfy with jQuery, go with jQuery - especially since majority of the time in AJAX functions is spent on server requests.

You'll find some interesting points in this question: When to use Vanilla JavaScript vs. jQuery?

And here's a funny one (still useful): https://meta.stackexchange.com/questions/45176/when-is-use-jquery-not-a-valid-answer-to-a-javascript-question

Community
  • 1
  • 1
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Also good to note that `$.ajax()` in jQuery comes with promises and deferred objects, something that the native `XMLHttpRequest` do not have. – Terry Dec 25 '14 at 14:08