为什么这个JavaScript只在IE资源管理器中工作

why this javascript only works in IE explorer

本文关键字:资源管理器 工作 IE 只在 JavaScript 为什么      更新时间:2023-09-26

嗨,我不知道为什么这个脚本只适用于Internet Explorer,正是在从Ajax返回的方法中调用。 看看脚本

function saveMap() {
if (confirm("Esta seguro de guardar el mapa?")) {
//        alert("Estas en el centro:" + map.getCenter().toString() + "Con zoom: " + map.getZoom().toString());
    var mapData = new Array(map.getCenter().lat().toString(),
                            map.getCenter().lng().toString(),
                            "Esto es una prueba",
                            map.getZoom().toString());
    $.ajax({
        type: "POST",
        url: "SaveMap.aspx/saveMapData",
        data: "{mapData: '" + mapData + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (flag) {
            //this block of code only works in IE
            if (flag)
                alert("Se guardo el mapa de manera correcta");
            else
                alert("Ocurrio un error en la ejecucion"); 
        }
    });    
}
}

这是我的方法在 aspx.net 中的签名

[WebMethod()]
public static bool saveMapData(string mapData) 
{ 
    //do something
    return true;
}

我知道为什么脚本在IE中工作,但在其他浏览器中不起作用。首先看看这里(也在这里),你会发现 json 从你那里返回 ASP.NET WebMethod 以 d 开头:

{"d":"something_json"}

所以在你的位置上,我会做这样的事情:

success: function (flag) {
            //this block of code only works in IE
            if (flag.d)
                alert("Se guardo el mapa de manera correcta");
            else
                alert("Ocurrio un error en la ejecucion"); 
        }

我可能认为这是因为IE是Microsoft软件,可以比其他浏览器更好地读取json和{"d":"something_json"}