Details of ajax-get () method examples in jQuery, jqueryajax
Use the get, post, and ajax methods in jquery to transmit data to the server.In the previous article, I shared with you the ajax-post () method example in jquery. Next I will continue to learn the ajax-get () method in jQuery through this article. For details, see the following.
JQuery Ajax Reference Manual
Instance
Use the ajax get request to change the text of the div element:
$("button").click(function(){ $.get("demo_ajax_load.txt", function(result){ $("div").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) {$ ('div. get '). append (dom)}); // send and display returned 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 File
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 is an example of the ajax-get () method in jQuery shared in this article. I hope you will like it.