Examples of ajax-get () in jQuery

Source: Internet
Author: User
In JQuery, you can use the get, post, and ajax methods to transmit data to the server. Next, we will introduce the ajax-get () method examples in jquery through this article, if you have any need, you can refer to the get, post, and ajax methods used in jquery to transmit data to the server. Next, we will continue to learn the ajax-get () method in jQuery through this article, for more information, see the following section.

Instance

Use ajax get requests to change the text of the p element:

$("button").click(function(){ $.get("demo_ajax_load.txt", function(result){ $("p").html(result); });});

Try it yourself

Definition and usage

The get () method loads information through a remote http get request.

This is a simple GET request function to replace complex $. ajax. You can call the callback function when the request is successful. To execute a function when an error occurs, use $. ajax.

Syntax

$(selector).get(url,data,success(response,status,xhr),dataType)

Detailed description

This function is short for Ajax functions and is equivalent:

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

The data returned to the success callback function varies depending on the MIME type of the response. The data can be an XML root element, text string, JavaScript file, or JSON object. You can also pass the response text status to the success callback function.

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

Example

Request the test. php webpage and ignore the returned value:

$. Get ("test. php ");

More examples

Example 1

Send two parameters to the test. php webpage and ignore the returned value:

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

Example 2

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

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

Example 3

Display the return value of test. cgi (HTML or XML, depending on the returned value), add a set of request parameters:

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

$. Get () usage of jquery ajax

Js files

$ (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'); // obtain the link to be submitted in Form var param = {}; // assemble the sending parameter param ['name'] = $ ('form input [name = name] '). val (); param ['age'] = $ ('form input [name = age] '). val (); $. get (url, param, function (dom) {$ ('p. get '). append (dom)}); // send and display returned content });})

Html file

This is the ajax get method.

PHP File

error_reporting(0); if($_GET["name"]=="kitty"){ $name= "you are the lucky";}else$name=$_GET["name"];$age=$_GET["age"];echo "

".$name." ".$age."

";

The above is an example of the ajax-get () method in jQuery shared in this article. I hope you will like it.

For more details about ajax-get () method examples in jQuery, please follow the PHP Chinese network!

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.