【问题标题】:How to save a variable to the server using jQuery如何使用 jQuery 将变量保存到服务器
【发布时间】:2015-06-07 01:39:28
【问题描述】:

我正在实施一个视频会议室,我有一个变量 (room_status),它保存房间的状态(0 = 关闭 & 1 = 打开)。现在这个变量只有点击 open-room 的客户才能访问。

我需要将 room_status 变量保存到服务器,以便其他客户端可以访问它。这是我的一段代码:

var room_status = 0; //room closed

$('#open-room').click(function () {
    // http://www.rtcmulticonnection.org/docs/open/
    $.ajax({
        type: 'GET',
        url: "../invite",
        data: {
            videoconference_id: $('#meetingID').val(),
            invitee_id: 1111,
            status: "Accepted"
        },
        success: function() {
            alert("success!");
        },
        error: function() {
            alert("fail");
        }
    });

    //room_status = 1; //room opened
    rmc.open();
    rmc.streams.mute({video : true});
    document.getElementById("on-off-video").style.color= 'red';
});

$('#join-room').click(function () {
    if(room_status) {
        // http://www.rtcmulticonnection.org/docs/connect/
        rmc.connect();
        rmc.streams.mute({video: true});
        document.getElementById("on-off-video").style.color= 'red';
    } else {
        console.log("Waiting for meeting organizer");
    }

});

【问题讨论】:

  • 答案将是:使用 ajax、$.ajax()、$.post()、$.get()。我认为您需要的不止这些,如果您已经拥有后端,那么您应该在此处提供它,以便我们知道要发送什么。如果不这样做,那么您应该在后端类别中询问如何。

标签: javascript php jquery yii webrtc


【解决方案1】:

Ajax 是您的朋友。 这是我的一个带有 jquery ui 的项目的示例:

function prepare_ajax_button(l){
    $("#button").button().click(function(event,ui){
        $.ajax({type: "GET",data: {"arg1":l},url: "update_variable.php",success: function(data){
            alert("Success ?!");
        },error: function(data){alert("Problem ?!");}});
    });
}

页面“update_variable.php”可以例如将变量写入文本文件,mysql...

【讨论】:

  • 这段代码有问题吗?我失败了... $.ajax({ type: 'GET', url: "../invite", data: { videoconference_id: $('#meetingID').val(),被邀请人_id: 1111, status: “接受”},成功:函数(){警报(“成功!”);},错误:函数(){警报(“失败”);}});
  • 看JavaScript控制台:有没有更有用的错误信息?查看开发者工具的 Net 标签:服务器是否返回 200OK 响应?
  • 我刚刚编辑了我的问题以反映我添加的内容。那个ajax片段有什么问题吗?我失败了……
猜你喜欢
  • 2016-10-23
  • 2023-02-14
  • 2017-11-02
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 2022-01-25
  • 2013-10-17
相关资源
最近更新 更多