如何在JavaScript方法中调用C#方法

时间:2018-07-14 20:45:52

标签: javascript c# jquery

我在用JavaScript调用C#方法时遇到麻烦。我正在使用Framework 2.0和Asp.net WebForms。我根本无法使其调用C#中的函数。这是我的代码:

[WebMethod]
public static List<string> GetMarkers()
{
    List<string> markers = new List<string>();
    string path = @"C:\Users\Utilizador\Desktop\PlaceMarks.xml";
    XmlDocument xml = new XmlDocument();
    xml.Load(path);

    XmlNodeList elemList = xml.GetElementsByTagName("Placemark");
    string name = string.Empty;
    string longi = string.Empty;
    string lat = string.Empty;
    string join;
    foreach (XmlNode elements in elemList)
    {
        //Marker marker = new Marker();
        name = elements["description"].InnerText;
        longi = elements["LookAt"].ChildNodes[0].InnerText;
        lat = elements["LookAt"].ChildNodes[1].InnerText;
        join = string.Format(name, longi, lat);

        markers.Add(join);
    }

    return markers;
}

protected void Button1_Click(object sender, EventArgs e)
{

}

这是我正在使用的xml(示例)

<Placemark id="pmID_01895782-3191-412e-8495-6ed551d19b52">
    <name>Buraco no pavimento</name>
    <description>
        <![CDATA[<b>Morada: </b>Avenida Fialho de Almeida 47, 7800 Beja, Portugal<br /><b>Categoria: </b>Conservação das Ruas e Pavimento<br /><b>Assunto: </b>Buraco no pavimento]]>
    </description>
    <LookAt>
        <longitude>-7.8649591</longitude>
        <latitude>38.0071425</latitude>
        <altitude>0</altitude>
        <range/>
        <tilt>0</tilt>
        <heading/>
        <altitudeMode>relativeToGround</altitudeMode>
    </LookAt>
    <styleUrl>#smID_d0a732ef-461d-406b-86b7-cc88cefcf09c</styleUrl>
    <Point>
        <coordinates>-7.8649591,38.0071425</coordinates>
    </Point>
</Placemark>

这是我的JavaScript代码:

<script type="text/javascript">

    function getEmployees() {
        $.ajax({
            type: "GET",
            url: 'CS.aspx/GetMarkers',
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
                Show(); // Show loader icon  
            },
            success: function (response) {


            complete: function () {
                Hide(); // Hide loader icon  
            },
            failure: function (jqXHR, textStatus, errorThrown) {
                alert("HTTP Status: " + jqXHR.status + "; Error Text: " + jqXHR.responseText); // Display error message  
            }
        });
    }
</script>

能帮我解决一下吗?也许我的代码中缺少某些内容。

0 个答案:

没有答案