copenndthagen
copenndthagen

Reputation: 50722

Question on jQuery AJAX

I have a few questions on jQuery AJAX.

  1. It is confusing to understand why there are multiple methods like load(), get(), post()..is the diff only like $.ajax is general way of writing and others being specific based on type..?

  2. I do not cleatly understand the diff between complete, success..Are they similar or is there any diff as to when each should be used ?

  3. In terms of script execution from within an HTML response, does jQuery AJAX handle it automatically OR do we need to specify something like eval() ? Also how diff is this behavior compared to a normal AJAX only handling?

  4. Regarding the beforeSend , is it similar to ajaxSetup and generally speaking, what are the common attributes which are used out of the many which are availbale?

Edited

  1. Also is the code written as callback for load()..e.g. load(url,function(){}); same as what is mentioned under success or ajaxSuccess..I mean will the callback function code not exectuted at the same time as the success or ajaxSuccess ?

Thnak you.

Upvotes: 2

Views: 101

Answers (2)

hvgotcodes
hvgotcodes

Reputation: 120168

1) you need to understand HTTP. get and post make "GET" and "POST" requests, respectively, which is useful if you are building a RESTful service. EDIT: I actually don't see get and post methods on the ajax object; you pass a 'type' parameter to specify the HTTP method you want to use.

2) success fires on success, i.e. if the response returns a 200. complete always fires after everything else is done.

3) Ideally, your server would return json. If you configure the Ajax call to expect json, then it will parse it for you.

4) The documentation is very clear, beforeSend is fired before the actual underlying ajax request is invoked. The documentation says things like "Use this to set custom headers, etc."

Upvotes: 11

mazlix
mazlix

Reputation: 6273

  1. They are just "shorthand" everything can be done and function the exact same with $.ajax(), the difference is only syntax
  2. complete is fired after every request is complete, while success only fires if there were no errors (a successfully one
  3. whatever you want to do with the HTTP response you do by making a function(data){dostuff(data);} in the success callback area
  4. beforeSend is called right before the ajax request is fired

Documentation

Upvotes: 0

Related Questions