Monday, December 16, 2013

Jquery: Displaying Object Property Names and Values

Jquery Displaying Object Property Names and Values

Here is one way to display a JSON object’s property name and value.  I used this to display the results of a test service call to a paging service.

C# class:
 /// 
    /// Container class to hold results of paging service calls
    /// 
    public class PagerComm
    {

        public string Address { getset; }
        public string Domain { getset; }
        public string ElaspedTime { getset; }
        public string Env { getset; }
        public string Error { getset; }
        
        
        public PagerComm()
        {

        }

        public PagerComm(string address, string domain, string elaspedTime, string env,string error)
        {
            this.Address = address;
            this.Domain = domain;
            this.ElaspedTime = elaspedTime;
            this.Env = env;
            this.Error = error;

        }





    }


Asmx method returning the class:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public PagingTools.PagerComm SendTestPage(string tstTyp, string pgrId, string pgrTyp)
    {
        PagingTools.PagerComm ret = new PagingTools.PagerComm();
 
        try
        {
 
            PagingTools.ETestPageService svc = new PagingTools.ETestPageService("local:dxf""test");
 
            ret = svc.TestEpicPaging(tstTyp, pgrId, pgrTyp);
            
            
            return ret;
        }
 
 
        catch (Exception e)
        {
            //TODO:  log e.message etc.
 
            return ret;
        }
 
 
    }
    




Service call:

function SendTestPage() {
    var serviceUrl = '..//Tools.asmx/SendTestPage';
    var tstTyp = $("#tstTypDdl option:selected").val();
    var pgrId = $("#pgrIdTxt").val();
    var pgrTyp = $("#pgrTypTxt").val();
 
    $.ajax({
        type: "POST",
        data: "{'tstTyp':'" + tstTyp + "', 'pgrId':'" + pgrId + "', 'pgrTyp':'" + pgrTyp + "'}",
        contentType: "application/json; charset=utf-8",
        url: serviceUrl,
        dataType: "json",
        success: function (response) {
            ParsePageComm(response.d);
        },
        error: function (response) {
            alert("failed service call" + serviceUrl);
        }
 
 
    });
 
}


Call back function:

function ParsePageComm(result) {
    if (result) {             
        $("#resultsDiv").html('');
        $.each(result, function (prpNm, prpVl) {
            if (prpNm.indexOf("_") < 0)
                $("#resultsDiv").html($("#resultsDiv").html() + prpNm + ": " + prpVl + "
");
        });
 
    }
    else {
        alert("Error with Service ");
    }
 
 
}




Example UI:

Address:
Domain:
ElaspedTime: 1004.805
Env:
Error: Failed to execute E3C command.


No comments: