问题描述
|
嗨,我不知道为什么这个脚本只能在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中工作,但不能在其他浏览器中工作。首先在这里(也在这里)看一看,您会发现从ASP.NET WebMethod返回的json以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\"}
。