IMA EricSun's Blog

JQuery Ajax Functions

Posted in Web Tech by Eric Sun on 2010/12/27

To use the jQuery post() function to send and receive data.

A simple post() function looks like this:

$.post( url, data, success())

Here are the parts of the post() function:

  • $.post: Begin the function.
  • url: Contains the path to the file on the Web server to which you want to send data. For example, the path might be to a PHP file that will e-mail form data to you when a visitor submits it.
  • data: Contains the data you want to send to the Web server. The information a visitor enters into a Web form, such as name and e-mail address, can be part of the data element.
  • success: Contains code that executes if the data was sent to the url successfully. For example, the code in the success() function can send a message to the person who filled out the form, letting him or her know the form was sent.

Using other AJAX functions:

✓ ajax(): Performs an AJAX request. This function can send and receive data from the Web server using either GET or POST.
✓ ajaxComplete(): Contains code to be executed when an AJAX request is completed.
✓ ajaxError():Containscodetobeexecuted if an AJAX request causes an error.
✓ ajaxSend(): Contains code to be executed before an AJAX request is sent.
✓ ajaxStart(): Contains code to be executed when an AJAX request begins.
✓ ajaxStop(): Contains code to be executed after all AJAX requests have stopped.
✓ ajaxSuccess(): Contains code to be executed when an AJAX request com- pletes successfully.
✓ get(): Performs an AJAX request. This function can send and receive data from the Web server using GET.
✓ getScript(): Loads a JavaScript file from the Web server.
✓ load(): Loads data from the Web. For example, load data from a text file or HTML file. As in the first example in this chapter, the loaded data can be displayed inside an element on the current page.
✓ post(): Performs an AJAX request. This function can send and receive data from the Web server using POST.
✓ serialize(): Grabs the values from a Web form and turn them into a single string of data to send using get(), post(), or ajax() function.