How to send post request with jquery

WBOY
Release: 2022-04-02 17:13:57
Original
8235 people have browsed it

In jquery, you can use the "$.post()" method to send a post request. The function of this method is to use an HTTP POST request to load data from the server. The syntax is "$(selector).post(URL, data, specifies the function to be run when the request is successful, data type)".

How to send post request with jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How jquery sends a post request

$.post() method uses HTTP POST request to load data from the server.

The syntax is:

$(selector).post(URL,data,function(data,status,xhr),dataType)
Copy after login

URL required. Specifies the URL to which requests are sent.

data Optional. Specifies the data to be sent to the server with the request.

function(data,status,xhr) Optional. Specifies a function to run when the request succeeds. Additional parameters:

  • data - Contains the result data from the request

  • status - Contains the status of the request ("success", "notmodified" , "error", "timeout", "parsererror")

  • xhr - Contains the XMLHttpRequest object

dataType Optional. Specifies the data type of the expected server response. By default, jQuery will figure it out intelligently. Possible types:

  • "xml" - an XML document

  • "html" - HTML as plain text

  • "text" - a plain text string

  • "script" - runs the response in JavaScript and returns it as plain text

  • "json" - Runs the response as JSON and returns it as a JavaScript object

  • "jsonp" - Loads a JSON chunk using JSONP, which will add a "?callback=?" to the URL to specify Callback

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input").keyup(function(){
txt=$("input").val();
$.post("demo_ajax_gethint.php",{suggest:txt},function(result){
$("span").html(result);
});
});
});
</script>
</head>
<body>
<p>在以下输入框中输入名字:</p>
第一个名称:
<input type="text" />
<p>匹配项: <span></span></p>
<p>该实例的PHP代码 (<a href="demo_ajax_gethint.txt" target="_blank">demo_ajax_gethint</a>) </p>
</body>
</html>
Copy after login

Output result:

How to send post request with jquery

Related video tutorial recommendations:jQuery video tutorial

The above is the detailed content of How to send post request with jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template