im尝试使用POST通过ajax jQuery发送一些数据到php文件,但我不断收到POST 405 Method Not Allowed错误,解决此问题的任何想法都会受到赞赏,这里是进行调用的功能
function manageData(key) {
var name = $("#countryName");
var abbrev = $("#countryAbbrev");
var desc = $("#countryDesc");
if (isNotEmpty(name) && isNotEmpty(abbrev) && isNotEmpty(desc)) {
$.ajax({
url: 'http://127.0.0.1:5500/ajax.php',
method: 'POST',
dataType: 'text',
data: {
key: key,
name: name.val(),
abbrev: abbrev.val(),
desc: desc.val()
},
success: function (response) {
alert(response);
}
});
}
}
这是ajax.php文件代码
<?php
if (isset($_POST['key'])) {
$conn = new mysqli(host:'localhost', username:'root', passwd:'root',
dbname:'mysqldatamanager');
$name = $conn->real_escape_string($_POST['name']);
$abbrev = $conn->real_escape_string($_POST['abbrev']);
$desc = $conn->real_escape_string($_POST['desc']);
if($_POST['key'] == 'addNew') {
$sql = $conn->query(query:"SELECT id FROM country WHERE
countryName = '$name'");
if ($sql->num_rows > 0) {
exit("Country already exists!");
} else {
$conn->query("INSERT INTO country (countryName,
countryAbbrev, countryDesc) VALUES ('$name', '$abbrev',
'$desc')");
exit("Country has been added succesfully!");
}
}
}
?>
答案 0 :(得分:-1)
请尝试以下代码。
function manageData(key) {
var name = $("#countryName");
var abbrev = $("#countryAbbrev");
var desc = $("#countryDesc");
if (isNotEmpty(name) && isNotEmpty(abbrev) && isNotEmpty(desc)) {
$.ajax({
url: 'http://localhost/ajax.php',
type: "POST",
data: {
key: key,
name: name.val(),
abbrev: abbrev.val(),
desc: desc.val()
},
success: function (response) {
alert(response);
}
});
}
}