gpt4 book ai didi

javascript - 关于jquery ajax,php文件已运行但成功功能不起作用

转载 作者:行者123 更新时间:2023-11-29 19:30:20 26 4
gpt4 key购买 nike

我的 html 文件

<script type="text/javascript">
$(document).ready(function () {
$('#thumb').click(function () {
event.preventDefault();
$.ajax({
url: "upvote.php?id=<?php echo $id ?>",
type: "get",
dataType: "json",
success: function (data) {
$("#test").html("hello");
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#test').html(msg);
},
});
});
});
</script>

upvote.php

<?php

session_start();
global $error;
global $conn;
include("connect.php");
$id = $_REQUEST['id'];
$uid = $_SESSION['uid'];
$sql = "SELECT voted FROM voted WHERE questionid=$id AND uid=$uid";
$tot = mysqli_query($conn, $sql);
$total = mysqli_num_rows($tot);
if ($total == 0) {
$query = mysqli_query($conn, "INSERT INTO voted(questionid, voted, uid) VALUES('$id','true','$uid')");
$query2 = "UPDATE question SET vote = vote + 1 WHERE questionid = '$id'";
mysqli_query($conn, $query2);
$query3 = "SELECT vote FROM question WHERE questionid = 'id'";
$ql = mysqli_query($conn, $query3);
$res = mysqli_fetch_array($ql);
$vote = $res['vote'];

json_encode($vote);
} else {
$false = "false";
json_encode($false);
}
?>

问题是,当单击$('#thumb')时,php文件正在运行,因为我可以发现其中的sql被执行,并且我可以在数据库MySQL中找到记录,但是,我想要一些在成功函数中运行的函数,我尝试尝试 $("#test").html("hello");但它不起作用。有什么建议吗,谢谢!

最佳答案

$(document).ready(function () {
$.ajax({
url: "upvote.php?id=<?php echo $id ?>",
type: "get",
dataType: "json",
success: function (data) {
alert('success '+data);
},
error: function (jqXHR, exception) {
alert('error '+exception);
},
});
});

关于javascript - 关于jquery ajax,php文件已运行但成功功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41823388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com