您好我想從後臺代碼中調用一個方法與jquery但它不工作,所以我需要幫助,因爲。從jquery調用aspx服務器方法的問題
這是JavaScript方法
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: '"+ map.getCenter().lat().toString() +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: response
});
}
}
這是服務器方法
[System.Web.Services.WebMethod]
public bool saveMapData(string mapData)
{
if((mapData != null) && (mapData.Length < 0))
{
throw new Exception("El centro y el zoom no deben ser nulos");
}
try
{
crearConexion();
SqlCommand _sqlCommand = new SqlCommand("INSERT INTO [AtentoMIG].[dbo].[Mapa]"
+ "([latitud]"
+ ",[longitud]"
+ ",[nombre]"
+ ",[zoom])"
+ "VALUES"
+ "('" + mapData[0] + "'"
+ ",'" + mapData[1]
+ ",'" + mapData[2]
+ "," + mapData[3] + "')", _sqlConexion);
_sqlCommand.ExecuteNonQuery();
_sqlConexion.Close();
return true;
}
catch (SqlException)
{
throw new Exception("Ocurrio un problema insertando la informacion en la base de datos");
}
finally
{
_sqlConexion.Close();
}
}
感謝的對你有所幫助
不工作,你是什麼意思?拋出異常?沒有響應更新?成功回調被調用嗎?還要添加失敗回調並查看回來的錯誤。 – 2011-05-23 13:41:45
@布賴恩不得到服務器端方法 – Jorge 2011-05-23 13:43:23
SaveMap.aspx/saveMapData是否爲正確的URL?嘗試使用像[fiddler](http://www.fiddler2.com/fiddler2/)這樣的工具來查看服務器響應是什麼。 – 2011-05-23 14:15:08