jquery Ajax-get () Method Example detailed _ajax related

Source: Internet
Author: User

using the Get,post and Ajax methods in jquery to deliver data to the server, in the previous article, we shared the Ajax-post () Method Example in jquery, and continue to learn the Ajax-get () method of jquery in this article, Please see below for specific introduction.

JQuery Ajax Reference Manual

Instance

Use AJAX get requests to change the text of a DIV element:

$ ("button"). Click (function () {
 $.get ("Demo_ajax_load.txt", function (Result) {
 $ (' div '). html (result);
 };
});

Give it a shot yourself.

Definitions and usage

The Get () method loads information through a remote HTTP GET request.

This is a simple GET request function to replace the complex $.ajax. Callback functions can be invoked when the request succeeds. If you need to perform a function when an error occurs, use $.ajax.

Grammar

$ (selector). Get (Url,data,success (RESPONSE,STATUS,XHR), DataType)

Detailed description

This function is shorthand for Ajax functions, equivalent to:

$.ajax ({
 url:url,
 data:data,
 success:success,
 datatype:datatype
});

Depending on the MIME type of the response, the return data passed to the success callback function is also different, which can be an XML root element, a text string, a JavaScript file, or a JSON object. You can also pass the response's text state to the success callback function.

For JQuery 1.4, you can also pass the XMLHttpRequest object to the success callback function.

Example

Request test.php Web page, ignore return value:

$.get ("test.php");

More examples

Example 1

Request test.php Web page, transfer 2 parameters, ignore return value:

$.get ("test.php", {name: "John", Time: "2pm"});

Example 2

Displays the test.php return value (HTML or XML, depending on the return value):

$.get ("test.php", function (data) {
 alert ("Data Loaded:" + data);
});

Example 3

Displays the test.cgi return value (HTML or XML, depending on the return value), and adds a set of request parameters:

$.get ("test.cgi", {name: "John", Time: "2pm"},
 function (data) {
 alert ("Data Loaded:" + data);
 });

jquery ajax $.get () usage detailed

JS file

$ (document). Ready (function () {
 $ ("form"). Submit (Function (event) {Event.preventdefault ()})//Cancel the default behavior of Submit
 $ ("form input[type= ' submit ']"). Click (function () {
 var url = $ (' form '). attr (' action ');//Take a link
 to submit in form var param = {}; Assembly send parameter
 param[' name ' = $ (' form Input[name=name] '). Val ();
 Param[' age '] = $ (' form Input[name=age] '). Val ();
 $.get (URL, param, function (DOM) {$ (' div.get '). Append (DOM)}); Send and display the return content
 });

HTML file

<form action= "ajax.php" method= "get" >
Name: <input type= "text" name= ' name '/> Age
: <input type= ' Text ' name= ' age '/>
<input type= "Submit"/>
</form>
<div class= "Get" > This is the Ajax get method </div>

PHP files

error_reporting (0);
 if ($_get["name"]== "Kitty")
{
 $name = "You are the lucky";
}
else
$name =$_get["name"];
$age =$_get["Age"];
echo "<div>". $name. " ". $age." </div> ";

The above introduction is this article gives everybody to share the jquery in Ajax-get () Method Example detailed explanation, hoped everybody likes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.