jQuery旋钮和POST请求从JavaScript

jQuery Knob and POST request from JavaScript

本文关键字:请求 JavaScript POST jQuery      更新时间:2023-09-26

如何与JavaScript和jQuery旋钮的参数形成一个标准的POST请求?

这是我得到的:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="jquery.knob.min.js"></script>
</head>
<body>
<input type="text" data-angleoffset=-125 data-anglearc=250 data-fgcolor="#66EE66" value="50" class="dial">
<script>
    $(".dial").knob({
    'release' : function (sendpostresp) {
        $.ajax({
            url: "publish.php", //the page containing php script
            type: "POST", //request type
            success: function (result) {
                alert(result);
            }
        });
    }
});
</script>
</body>
</html>

我需要保存释放旋钮的值。PHP部分等待POST并将值保存到数据库中,并且它工作。

在我看来,我的代码应该用POST将旋钮的当前值发送到PHP脚本。但是我看到控制台没有参数,只有一个空的POST响应。

不幸的是,官方的jQuery Knob文档没有提供足够的说明。请帮助我的代码示例,如何发送当前值从jQuery旋钮通过POST ?

您只需要在ajax请求中添加一个data属性。它可以像这样:

$(".dial").knob({
    'release' : function (sendpostresp) {
        $.ajax({
            url: "publish.php", //the page containing php script
            type: "POST", //request type
            data: { 
                foo: sendpostresp 
            },
            success: function (result) {
                alert(result);
            }
        });
    }
});

根据Knob文档,"释放"回调函数中的参数是拨号的当前值。只需将其粘贴到ajax请求的数据对象中,它将被发送到处理请求的服务器端代码