在ajax请求中使用JSON响应更新JQuery Progressbar

时间:2010-04-28 18:16:15

标签: php json jquery-ui jquery

所有

我有一个AJAX请求,它向服务器发出JSON请求,以获取同步状态。 JSON请求和响应如下:我想显示JQuery UI进度条并根据getStatus JSON响应中返回的百分比更新进度条状态。如果状态为“insync”,则不应显示进度条,而应显示消息。例如:“服务器处于同步状态”。我怎么能这样做?

//JSON Request to getStatus
{
    "header": {
        "type": "request"
    },
    "payload": [
        {
            "data": null,
            "header": {
                "action": "load",
            }
        }
    ]
}

//JSON Response of getStatus (When status not 100%)
{
    "header": {
        "type": "response",
        "result": 400
    },
    "payload": [
        {
            "header": {
                "result": 400
            },
            "data": {
                "status": "pending",
                "percent": 20
            }
        }
    ]
}

//JSON Response of getStatus (When percent is 100%)
{
    "header": {
        "type": "response",
        "result": 400
    },
    "payload": [
        {
            "header": {
                "result": 400
            },
            "data": {
                "status": "insync"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

假设您希望将进度条/消息放在名为“loadingDiv”的div中:

$(document).ready(function() {
  var myLoadingDiv = $("#loadingDiv");
  myLoadingDiv.progressbar({disabled:true});
  $.getJSON("getStatus.php", function(data) {
    if (data.payload.data.status == "insync") {
      myLoadingDiv.progressbar("disable");
      myLoadingDiv.html("Server is in Sync");
    }
    else if (data.payload.data.status == "pending") {
      myLoadingDiv.progressbar("enable");
      myLoadingDiv.progressbar("value", data.payload.data.percent);
    }
    else {
      //something else if there are any other status'
    }
  });
});