[英]differentiating between post requests
I have this jquery function which sends a POST request to the server我有这个 jquery function 向服务器发送 POST 请求
function loadInfo() {
jQuery(function($) {
$.ajax({
method: "POST",
url: "/admin.php",
dataType: "json",
success: function(data) {
console.log(data);
for (var i = 0; i < data.length; i++) {
setMarker(data, i, "red");
printInfo(data, i);
}
}
})
});
}
This is how the request gets handled这就是处理请求的方式
if($_SERVER['REQUEST_METHOD'] === 'POST'){
header('Content-Type: application/json');
require 'private/database.php';
$sql = "SELECT * FROM form";
$result = mysqli_query($conn, $sql);
$data = array();
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
}
die(json_encode($data));
}
The code works, but what if I have send multiple POST requests to the same server?该代码有效,但是如果我向同一台服务器发送了多个 POST 请求怎么办? Is there a way to differentiate between them like how you would when handling HTML forms?有没有办法像处理 HTML forms 时那样区分它们? eg If the of the button that submits the form is "submit_button"...例如,如果提交表单的按钮是“submit_button”...
if (isset($_POST['submit_button'])) {
...
}
You have to send some parameter to differentiate the request.您必须发送一些参数来区分请求。
eg: $.post( "test.php", { name: "John", time: "2pm", method:"formSubmit" } );
There are a number of ways to send post by AJAX with data (multiple data items). AJAX 可以通过多种方式发送带有数据(多个数据项)的帖子。 One of the ways is:其中一种方法是:
$.ajax({
url: 'process.php',
type: 'POST',
data: {status: "ok", name: "Richard"},
success: function (result) {
alert('success');
}
})
In the above case, you have sent status and name data to the php script by POST method.在上述情况下,您已通过 POST 方法将状态和名称数据发送到 php 脚本。 Hope that it can answer your question.希望它能回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.