开发者

Trying to create a WebService to return a 3D Array to populate table with javascript

开发者 https://www.devze.com 2022-12-19 09:06 出处:网络
I am trying to create a dynamic table that is being built based on the value of a search text box.The Table will have probably 6 columns of 20(max) rows.

I am trying to create a dynamic table that is being built based on the value of a search text box. The Table will have probably 6 columns of 20(max) rows.

My problem is I can't figure out how to return the data from the web-service to the JS function in a way that will allow me to extract the data that I need.

Here is the web-service method I have currently:

[WebMethod]
public v_EMU_AIR_AIR[] GetCompletionList(string prefixText)
{
    NeoDataContext dataContext = new NeoDataContext();

    var results = from a in dataContext.GetTable<v_EMU_AIR_AIR>()
                  where a.Report_Date_ID.StartsWith(prefixText) ||
                  a.Item_Description.Contains(prefixText) ||
                  a.Drw_Nbr.StartsWith(prefixText)
                  select a;

    return results.ToArray<v_EMU_AIR_AIR>();

}

This will return an Array my objects, but in my javascript:

populateTable:function(returnList) {

    var length = returnList.childNodes[0].childNodes.length

    for(var i=0; i<length; i++) {

  开发者_开发问答      var textValue = returnList.childNodes[0].childNodes[i].textContent

         $("<tr><td>" + textValue + "</td></tr>").insertAfter(this.tblResults[0].childNodes[1]);                   
    }
}

All I can produce is a dump of the entire Object and can't pull out the specific fields that I need.


I would serialize your object using Javascript Object Notation (JSON).

Unfortunately, this feature isn't built in to the .NET framework, so you'll need to write the code yourself.

Fortunately, most of it is already done for you. Check out this link or this link for more information.

In your javascript, you can retreive the JSON object with jquery (documentation) and access the values of your properties like you would in C#

$(document).ready(function() {
    $.getJSON("http://url.to.webservice.com/", function(jsonObj) {
        alert(jsonObj.property);
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消