jQuery AJAX get() and post() methods

jQuery get() and post() methods are used to request data from the server via HTTP GET or POST requests.

HTTP request: GET vs. POST

Two commonly used methods for request-response between the client and server are: GET and POST.

  • GET - Request data from the specified resource
  • POST - Submit the data to be processed to the specified resource

GET is basically used to obtain (retrieve) data from the server. Note: The GET method may return cached data.

POST can also be used to retrieve data from the server. However, the POST method does not cache data and is often used to send data along with the request.

To learn more about GET and POST and the differences between the two methods, please read our HTTP method - GET vs. POST.

jQuery $.get() method

The $.get() method retrieves data from the server using an HTTP GET request.

语法:

$.get(URL,callback);

必需的 URL 参数规定您希望请求的 URL。

dangane callback almomomin na yau na yau ya kasance jumla aiki na aiki bayan shingen.

The following example uses the $.get() method to retrieve data from a file on the server:

duba

$("button").click(function(){
  $.get("demo_test.asp",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

sai ya kai jumla

The first parameter of $.get() is the URL we want to request ("demo_test.asp").

The second parameter is the callback function. The first callback parameter contains the content of the requested page, and the second callback parameter contains the status of the request.

kafi:this ASP file ("demo_test.asp") is like this:

<%
response.write("This is some text from an external ASP file.")
%>

jQuery $.post() 方法

$.post() 方法通过 HTTP POST 请求从服务器上请求数据。

语法:

$.post(URL,data,callback);

必需的 URL 参数规定您希望请求的 URL。

dangane data 参数规定连同请求发送的数据。

dangane callback almomomin na yau na yau ya kasance jumla aiki na aiki bayan shingen.

sannan yana aiki $.post() a kai sadaukar da data da shingen:

duba

$("button").click(function(){
  $.post("demo_test_post.asp",
  {
    name:"Donald Duck",
    city:"Duckburg"
  },
  function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

sai ya kai jumla

$.post() na almomomin na farko ya kasance URL na aiki na neman ("demo_test_post.asp").

sannan a kai tsara data da shingen (name da city) a kai sadaukar da su.

"demo_test_post.asp" na ASP aiki ya bincika wannan almomomin, ya aiki kan su, kuma ya koma aiki.

almomomin na uku na yau ya kasance jumla aiki. almomomin na farko na jumla aiki ya kuma da dukiyarin wanda a kai sadaukar da shi, kuma almomomin na biyu ya kuma da hali na shingen.

kafi:wannan ASP fiye da ("demo_test_post.asp") ya kamata kamar yadda:

<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & ". ")
Response.Write("Hope you live well in " & city & ".")
%>

jQuery AJAX kwarewar yinwa

Domin samun kwarewar yinwa aiki AJAX a kammala, za a kai tsarafin jQuery AJAX kwarewar yinwa.