Home Web Front-end JS Tutorial Ajax_POST submission

Ajax_POST submission

Jun 29, 2018 pm 02:57 PM

Ajax_POST submission

* $_post(): jquery handles post requests in ajax

* Basic syntax: $.post(url, data, success, dataType)

* Parameter description:

* url: requested address

* data: data that needs to be sent to the server

* success(data, status, xhr): execution Successful callback function,

* Callback parameters: 1.data: Data returned from the server

* 2.status: Status of the current request

* 3.xhr : ajax object

* Usually we only care about the returned data: data

* dataType: format of data returned from the server

* xml, html, script, json, text , _default

* Usually 'json', which can be omitted and automatically determined by the system

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>4.Ajax_POST</title>
</head>
<body>
<form action="api/check.php" method="post">
<fieldset>
<legend>用户登录</legend>
<p>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email">
</p>
<p>
<label for="password">邮箱:</label>
<input type="password" name="password" id="password">
</p>
<p>
<button>登录</button>
<span id="tips" style="font-size:1.2em;font-weight: bolder;color:red"></span>
</p>
</fieldset>
</form>
</body>
</html>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(&#39;button:first&#39;).click (function(){
//1.ajax-post提交的地址
var url = &#39;api/user.php?m=login&#39;
//2.要提交到服务器的数据
var data = {
"email": $(&#39;#email&#39;).val(),
"password": $(&#39;#password&#39;).val()
}
//3.设置执行成功的回调函数
var success = function(res){
if (res == &#39;1&#39;) {
$(&#39;#tips&#39;).text(&#39;登录成功,正在跳转中...&#39;)
setTimeout(function(){
location.href = &#39;api/index.php&#39;
},2000)
} else {
$(&#39;#tips&#39;).text(&#39;邮箱或密码错误,请重新输入...&#39;)
$(&#39;#email&#39;).focus()
setTimeout("$(&#39;#tips&#39;).empty()",2000)
}
}
//4.设置返回的数据格式为:json
var dataType = &#39;json&#39;
//5.调用全局函数$.post()执行post请求
$.post(url, data, success, dataType)
/**简化代码,将参数值直接写到参数中
$.post(
&#39;api/user.php?m=login&#39;,
{
"email": $(&#39;#email&#39;).val(),
"password": $(&#39;#password&#39;).val()
}, 
function(data){
if (data == &#39;1&#39;) {
$(&#39;#tips&#39;).text(&#39;登录成功,正在跳转中...&#39;)
setTimeout(function(){
location.href = &#39;api/index.php&#39;
},2000)
} else {
$(&#39;#tips&#39;).text(&#39;邮箱或密码错误,请重新输入...&#39;)
$(&#39;#email&#39;).focus()
setTimeout("$(&#39;#tips&#39;).empty()",2000)
}
}, &#39;json&#39;)
*/
//禁用默认提交
return false
})
</script>
Copy after login

The above is the detailed content of Ajax_POST submission. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to extend the timeout of Ajax requests? How to extend the timeout of Ajax requests? Jan 26, 2024 am 10:09 AM

How to extend the expiration time of Ajax requests? When making network requests, we often encounter situations where we need to process large amounts of data or complex calculations, which may cause the request to time out and fail to return data normally. In order to solve this problem, we can ensure that the request can be completed successfully by extending the expiration time of the Ajax request. The following will introduce some methods and specific code examples to extend the expiration time of Ajax requests. When making an Ajax request using the timeout attribute, you can set the timeout attribute to

How long does it take for ajax requests to expire? How long does it take for ajax requests to expire? Nov 20, 2023 am 10:29 AM

AJAX requests have no fixed expiration time: "Asynchronous JavaScript and XML" is a technology for sending asynchronous requests on web pages, which uses JavaScript to send requests to the server and receive responses without refreshing the entire page.

How to use controllers to handle Ajax requests in the Yii framework How to use controllers to handle Ajax requests in the Yii framework Jul 28, 2023 pm 07:37 PM

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework

How to choose the right Ajax request library for your project How to choose the right Ajax request library for your project Jan 30, 2024 am 08:32 AM

Practical guide: Which Ajax request libraries are suitable for your project? With the continuous development of front-end development, Ajax has become an indispensable part of web development. Choosing an Ajax request library suitable for the project is crucial to improving development efficiency and optimizing user experience. This article will introduce several commonly used Ajax request libraries to help readers choose the tool suitable for their own projects. jQueryAjax There is no denying that jQuery is one of the most popular JavaScript libraries out there. It provides a rich

Essential tools: Understand what are the commonly used Ajax request libraries? Essential tools: Understand what are the commonly used Ajax request libraries? Jan 30, 2024 am 11:00 AM

Development essentials: Explore what are the commonly used Ajax request libraries? In modern front-end development, using Ajax for asynchronous requests has become a standard feature, and choosing an appropriate Ajax request library can allow us to process network requests more efficiently, improving development efficiency and user experience. This article will explore some commonly used Ajax request libraries to help developers choose the tools suitable for their projects. jQueryAjax: As one of the most popular JavaScript libraries, jQuery provides powerful Ajax request functions.

Can I customize the expiration time of Ajax requests? Can I customize the expiration time of Ajax requests? Jan 26, 2024 am 11:13 AM

Can the expiration time of Ajax requests be customized? In web development, we often use Ajax to implement asynchronous requests to dynamically load data in the page. When making Ajax requests, sometimes we need to control the timeout of the request, that is, set a time limit, and process it if no response is received within the specified time. So, can the expiration time of Ajax requests be customized? This article will introduce this problem in detail and provide specific code examples. Using jQuery's Ajax function

What causes an Ajax request to timeout? What causes an Ajax request to timeout? Jan 26, 2024 am 10:53 AM

Under what circumstances will an Ajax request expire? With the development of Web applications, Ajax (Asynchronous JavaScript and XML) technology has become an essential part of Web development. Through Ajax, we can obtain data from the server and dynamically update the content of the web page without refreshing the entire page. However, when using Ajax to send requests, sometimes you encounter request expiration. So, under what circumstances will an Ajax request expire?

How to set the timeout for Ajax requests? How to set the timeout for Ajax requests? Jan 26, 2024 am 09:23 AM

How to set the expiration time of Ajax request? Need specific code examples With the development of Internet applications, Ajax has become an indispensable part of Web development. When sending an Ajax request, sometimes we need to limit the expiration time of the request to prevent the request from being too long, resulting in poor user experience or browser freezes. This article will introduce in detail how to set the expiration time of Ajax requests and give specific code examples. Setting the expiration time of Ajax requests mainly requires XMLHttpRequest

See all articles