0

I'm Developing an windows store apps using javascript. I have deployed WCF service and able get data from the wcf service.My problem is the data is unable to bind to controls(here i'm having only one dropdown box).

<body onload="onLoad()">

 <select id="CbxArea" style="width: 200px" data-win-bind="textContent: AreaName">
    <option>Select Area</option>
</select>
<script type="text/javascript">
    function onLoad() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {                
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        //var Items = JSON.parse(xmlhttp.responseText);<--Frist Way
                var Items = window.toStaticHTML(xmlhttp.responseText);<--Second Way

                for(var i=0;i<Items.length;i++)
                {
                document.getElementById('CbxArea').innerHTML = '<option>' + Items+'</option>';
                }
            }
            else {
                document.getElementById('CbxArea').innerHTML = '<option>' + 'Error' + '</option>';
            }
        }
        xmlhttp.open("POST", "WCFService_url", true);
        xmlhttp.send();

    }
</script>
</body>

i have tried using json.parse and windows.toStaticHTML but i'm unable to bind the data to dropdown. Please any provide me any solution is much appreciated.

4

1 回答 1

0

首先,您需要定义 WCF,如下所示

 [OperationContract]
 [WebInvoke(UriTemplate = "/GetAreaNames", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
 List<Area> GetAreaNames();

或对于多个参数

[OperationContract]
[WebInvoke(UriTemplate = "/ExcelDataInfo?area={area}&month={month}", RequestFormat =WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
   void ExcelDataInfo(string area, string month);

之后在脚本页面中

var xmlhttp;

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var Items = JSON.parse(xmlhttp.responseText);
        var data = Items;
        var options;
        var length = Items.GetAreaNamesResult.length;
        for (var i = 0; i < length; i++) {
            $("#CbxArea").append("<option>" + Items.GetAreaNamesResult[i].AreaName + "</option>");//GetAreaNamesReult is your method and AreaName is variable
        }
        document.getElementById('CbxArea').selectedIndex = 1;
    }
    else {
        document.getElementById('CbxArea').innerHTML = '<option>Select Area</option>';
    }
}
xmlhttp.open("POST", "WCFService_url", true);
xmlhttp.send();

用于传递多个参数,如下所示

  var url = "http://localohost:8090/ExcelDataInfo?area=" + area + "&month=" + month;
于 2013-06-25T11:57:38.690 回答