我正在尝试在按钮的onclick事件中调用一个带有一些参数的PHP文件。它执行到jsfile.js中的alert语句。但是之后ajax部分没有被执行...请帮帮我。
main.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="jsfile.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<button onclick="cart(0)"> hi </button>
<p id="disp"></p>
</body>
</html>
jsfile.js
function cart(id1) {
var id = id1;
alert("enterd " + id);
document.getElementById("disp").innerHTML = "hi";
$.ajax({
url: "/add.php ",
type: "POST",
data: {
item_id: id,
},
success: function(response) {
// document.getElementById("total_items").value = response;
document.getElementById("disp").innerHTML = response;
},
error: function() {
alert("error");
}
});
}
add.php
<?php
if(isset($_POST['item_id']) && !empty($_POST['item_id'])) {
// if(count($_POST)>0)
echo "success";
exit();
}
?>
url:"/add.php ",
这个 add.php 文件是否位于网站根目录中? - bloodyKnuckles