I am parsing generated json with jquery $.ajax but there is one option that I dont understand. I saw it in some examples and tried to look for at jquery.com but still not sure about it:
this option is:
data: { get_param: 'value' }
which is used like this:
$.ajax({
type: 'GET',
url: 'http://example/functions.php',
data: { get_param: 'value' }, //why we shell use that in that case?
success: function (data) {
var names = data
$('#cand').html(data);
}
});
I know that “data:” is what sent to the server but parsing JSON I thought i don’t send but retrieve from server with GET type. And the next part “get_param: ‘value’”
does not make sense to me in that case either, could anyone please explain when and what for and in what cases it shell be used?
thank you.
Yes. If
datais an object, it gets serialized to anapplication/x-www-form-urlencodedstring and then placed in the query string or request body as appropriate for the request type (GET/POST).jQuery does all the escaping necessary for this.
(It also, by default, collapses nested data structures (you don’t have any in your example) into PHP-style by adding
[]to key names).JSON is not involved (unless the server responds with some).
Whenever you want to pass data to the server rather than requesting a static URI.