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

标签 javascript php jquery mysql ajax

我的 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/

相关文章:

javascript - 一页上有多个选项卡部分

php - 计算点击次数并将其存储在 FTP 中

javascript - 箭头键在内容内不起作用可编辑

javascript - 如何使用内部 div 拖动其外部父 div?

javascript - Krajee Bootstrap 文件输入无响应

javascript - 使用 PHP 变量作为 Javascript 函数参数

javascript - 不要使用后退按钮让弹出窗口出现

php - 调用未定义的方法 Gallery::newQuery() Laravel

php - 当我进入 php 页面时,php 程序如何决定首先调用哪个函数?

javascript - 如何启用和禁用页面滚动