将方法从webservice设置为javascript

Set method from webservice to javascript

本文关键字:javascript 设置 webservice 方法      更新时间:2023-11-01

我在javascript中有这个函数,它为每个标记设置所有标记和弹出信息。

var infowindow = new google.maps.InfoWindow();
for (var i = 0; i < arraylng.length - 1; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(arraylng[i], arraylat[i])
    });
    var infowindow = new google.maps.InfoWindow({
        content: " "
    });
    makeInfoWindowEvent(map, infowindow, marker, i);
    markers.push(marker);
}
function makeInfoWindowEvent(map, infowindow, marker, i) {
    google.maps.event.addListener(marker, 'click', function() {
        alert(arraylat[i]); // working nice 
        alert(arraylng[i]); // working nice 
        infowindow.setContent(arraylng[i] + ", " + arraylat[i]);
        infowindow.open(map, marker);
    });
}
                }

我的问题是:如何从Web服务添加我的方法,Web服务从数据库返回地址?这是代码。

[WebMethod]
public string GetAddressMarker(string lat, string lng) {
    string address = "";
    var plac = GoogleapiBO.getClinicByLatLng(lat, lng);
    address = plac.address + ", " + plac.city;
    address = "'" + address + "'";
    return address;
}

我只需要从这个方法(方法需要string lat=arraylat[i] & string lng=arraylng[i])中获取一个地址,并将其设置为infowindow.setContent(*here*);

我该怎么做?

解决方案#1

网络服务:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetAddressMarker(string lat, string lng) {
    string address = "";
    var plac = GoogleapiBO.getClinicByLatLng(lat, lng);
    address = plac.address + ", " + plac.city;
    address = "'" + address + "'";
    return address;
}

Javascript

function makeRequest(url, message) {
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    httpRequest.onreadystatechange = getResponse;
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    httpRequest.send(message);
}
function getResponse() {
    if (httpRequest.readyState == 4 && httpRequest.status == 200) {
        //convert JSON string into object
        progressInfo(JSON.parse(httpRequest.responseText)) 
    }
}
function progressInfo(info) {
    //doing something with object info - in your case it should be the string
}

调用方法:

makeRequest("http://..../webservice/Service.asmx/GetAdressMarker");

解决方案#2

在c#代码中设置一个javascript变量:

<script type="text/javascript">
     var address= '<%= _address%>';
</script>

代码背后:

public partial class Entscheidungen : System.Web.UI.Page
{
    private string _address;
    protected void Page_Load(object sender, EventArgs e)
    {
         bt.Command += new CommandEventHandler(bt_Command);
    }
    void bt_Command(object sender, CommandEventArgs e)
    {
         _address = Service.GetAddressMarker(string lat, string lng)
    }
    ....
}

试试这个,它可能对u 使用完全

<script type="text/javascript" language="Javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js"></script>

<script language="javascript" type="text/javascript">
    var webServiceUrl = "../MathService.asmx/GetFullName";
    $("#btnAjax").click(function() {
        var webServiceUrl = "../MathService.asmx/GetFullName";
        var name = $("#txtBoxName").val();
        $.ajax({
            type: "POST",
            url: webServiceUrl,
            data: "{'name':'" + name + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: SuccessCallBack,
            error: FailureCallBack
        });
    });
    function SuccessCallBack(data) {
        alert(data.d);
    }
    function FailureCallBack(data) {
        alert(data.staus + " : " + data.statusText);
    }</script>

不要忘记将这个属性添加到服务中:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetAddressMarker(string lat, string lng) {
    string address = "";
    var plac = GoogleapiBO.getClinicByLatLng(lat, lng);
    address = plac.address + ", " + plac.city;
    address = "'" + address + "'";
    return address;

}