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=jkl). 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/