To send a PUT request using AJAX and PHP, you can use the following code:
AJAX code:
var data = {
name: "John",
age: 25
};
$.ajax({
url: "your_php_file.php",
type: "PUT",
data: data,
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
}
});
PHP code (in your_php_file.php):
<?php
// Read the PUT data
parse_str(file_get_contents("php://input"), $putData);
// Access the PUT data
$name = $putData['name'];
$age = $putData['age'];
// Perform necessary operations with the data
// Return a response
echo "PUT request successful";
?>
In this example, we are sending a PUT request to the your_php_file.php
file with some data (name and age). In the PHP file, we use parse_str
to read the PUT data and then access it using the corresponding keys. After performing any necessary operations, we can return a response back to the AJAX success function.
上一篇:php 得到文件名
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站