jQuery AJAX Shorthand Methods

jQuery AJAX Shorthand Methods

In ajax, get and post are two common submission methods. Formats json is often used.
So jQuery encapsulates them further to make them easier to use.


jQuery AJAX Shorthand Methods List

methoddescription
jQuery.get()Load data from the server using a HTTP GET request.
jQuery.getJSON()Load JSON-encoded data from the server using a GET HTTP request.
jQuery.getScript()Load a javascript file from the server using a GET HTTP request, then execute it.
jQuery.post()Load data from the server using a HTTP POST request.
.load()Load data from the server and place the returned HTML into the matched element.

Examples:

jQuery.get()

// use HTTP GET mehtod to request test.php, ingore the data from the server
$.get("test.php");
// request test.php with two parameters
$.get("test.php", { name: "John", time: "2pm" } );
// use a callback function to process the returned data
$.get("test.php", function(data){
  alert("Data Loaded: " + data);
});

jQuery.getJSON()

// Load JSON data via HTTP GET request and show it
$.getJSON("test.php", function(json){
  alert("JSON Data: " + json.users[3].name);
});

jQuery.getScript()

// Load and execute test. js, and display information after success.

$.getScript("test.js", function(){
  alert("Script loaded and executed.");
});

jQuery.post()

just like jQuery.get(),but use HTTP POST request

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

// Parse the  data from server using json format 
$.post("test.php", { "func": "getNameAndTime" },
   function(data){
     alert(data.name); // John
     console.log(data.time); //  2pm
}, "json");

.load()

Load the contents of the feeds.html.
$("#feeds").load("feeds.html");
date2019-04-03 00:47:53 From:www.jquerypluginhub.com Author:JPH
Related Tags:
Comments

Leave a Reply

Your email address will not be published. Required fields are marked *