views:

343

answers:

1

All,

I have an AJAX request, which makes a JSON request to a server, to get the sync status. The JSON Request and responses are as under: I want to display a JQuery UI progressbar and update the progressbar status, as per the percentage returned in the getStatus JSON response. If the status is "insync", then the progressbar should not appear and a message should be displayed instead. Ex: "Server is in Sync". How can I do this?

//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"
            }
        }
    ]
}
A: 

Assuming you want your progress bar/message to be placed in a div named "loadingDiv":

$(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'
    }
  });
});
GlenCrawford