$.ajax("url", {
option : value,
option : value,
option : value,
...
});
$.ajax("http://www.example.com/foo.php", {
data: { username: 'foobar', password: 'abcdef' }, // "password=abcdef"
success: loginSuccess,
error: loginFailure,
complete: dismissDialog // will be called in either case
});
Default request is get. Make it a post by adding type: "post" to options.
$.ajax("url", {
option : value,
...
});
$.ajax("http://www.example.com/foo.php", {
type: "post",
success: loginSuccess,
...
});
jQuery also has
$.get
and
$.post
options if you are too lazy to include this option.
For security reason we are not allowed to perform ajax requests to other domains. This causes a problem: Other domains have all the cool data!
There are 2 ways to work around this
dataType: 'jsonp'
as an ajax option.
JSONP - Basically involves an agreement between developers that we want to be able to share data so lets use a script workaround.
Full explaination on INFO 343 lecture slides link
// get the value from the text box with an id of search
var query = $('#search').val();
$.ajax('https://search.twitter.com/search.json', {
data: { 'q': query},
dataType: 'jsonp',
success: injectTweets,
error: ajaxError
});
/* When given json information as data inject
that information into the page. Assumes there exists
a ul in the page with an id of results. */
function injectTweets(data) {
var res = data.results; // get all the results of the request.
var dl = $('#results');
dl.empty();
// display each result on the page.
$.each(res, function() {
// get the username and tweet from the json data
var dt = $("<dt>").text(this["from_user"] + " says:");
var dd = $("<dd>").text(this["text"]);
dl.append(dt, dd);
});
}