Just to add my £0.02 worth: it's not essential to use $.ajax if you want to
handle errors.
There's a function $.ajaxSetup() available for making global modifications
to the default parameters such as error handler.

I have a globally included error handler for all pages set up like so:

$(document).ready(function() {
  $.ajaxSetup({error: on_ajax_error});
});

function on_ajax_error(xhr, error, exception) {
  // ...
}

Hope that's worth something to you :-)
rob.


On 4/28/07, Dave Cardwell <[EMAIL PROTECTED]> wrote:


wyo wrote:
> What's the difference of $.getJSON versus $.get? I currently use
> $.getJSON to load JSON data but I can't see any advantages.
>
> Is $.getJSON just another way for
>
>  $.ajax({
>    type: "GET",
>    url: "link name",
>    dataType: "json",
>    complete: "function name"
>  })
>
> or is the callback excuted only when "success"?
>
> What means "data" in the POST case? How could I access this data on
> the server, e.g. in PHP?
>
> O. Wyss
>

Both $.get and $.getJSON are "shortcuts" for $.ajax, for use when you
don't need the more complex features. $.getJSON explicitly sets the
dataType to "json", as you speculated above, while $.get does not.

For $.get, the callback function will be passed a string containing what
was returned from the request. For $.getJSON however, the callback
function will be passed the data structure that the JSON represented
(see Wikipedia[1] and/or the jQuery docs[2] for more on that).

In the case of $.get and $.getJSON, the callback function is only used
when a successful request was made. If you want to handle errors as
well, you will need to use the more complex $.ajax. The "AJAX" page[3]
of the jQuery docs explains all this in detail.

For GET requests the data is passed as URL params (e.g.,
http://foo.com/bar.php?abc=def&ghi=jkl151). If you set the request method
to post, it issues a normal HTTP POST (as if you had, for example,
submitted a normal HTML form). How to access these on the server side
does depend on your language. I'm not a PHP programmer, but I think you
might access them through the $_GET and $_POST arrays.


[1]http://en.wikipedia.org/wiki/JSON

[2]http://docs.jquery.com/Ajax#.24.getJSON.28_url.2C_params.2C_callback_.29
[3]http://docs.jquery.com/Ajax


--
Best wishes,
Dave Cardwell.

http://davecardwell.co.uk/javascript/jquery/152




--
Rob Desbois
Eml: [EMAIL PROTECTED]
Tel: 01452 760631
Mob: 07946 705987
"There's a whale there's a whale there's a whale fish" he cried, and the
whale was in full view.
...Then ooh welcome. Ahhh. Ooh mug welcome.

Reply via email to