2011-05-23 51 views
0

您好我想從後臺代碼中調用一個方法與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(); 
     } 

    } 

感謝的對你有所幫助

+0

不工作,你是什麼意思?拋出異常?沒有響應更新?成功回調被調用嗎?還要添加失敗回調並查看回來的錯誤。 – 2011-05-23 13:41:45

+0

@布賴恩不得到服務器端方法 – Jorge 2011-05-23 13:43:23

+0

SaveMap.aspx/saveMapData是否爲正確的URL?嘗試使用像[fiddler](http://www.fiddler2.com/fiddler2/)這樣的工具來查看服務器響應是什麼。 – 2011-05-23 14:15:08

回答

0

我解決,只有兩個變化問題在我後面的代碼

[WebMethod()] //this line change 
public static bool saveMapData(string mapData) // this line 
{   
    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(); 
    } 

} 
其餘

是相同的還有與URL或JSON數據沒有問題。但是由於網頁上的所有反正

1

我平時用的JSON2.js.stringify()方法包括以確保我的JSON是cor矩形的,但你可以嘗試改變你的數據線這樣的:

data:'{"mapData": "'+ map.getCenter().lat().toString() +'"}', 
0

嘗試使用JavaScript函數,它們會自動由ASP.NET運行時創建的ASP.NET調用它,它很適合我

MyNameSpace.MyClass.saveMapData(mapData,fnSaveMapData_Success,fnSaveMapData_Error);

2

Web方法必須是靜態的:

變化:

[System.Web.Services.WebMethod] 
public bool saveMapData(string mapData) 

要:

[System.Web.Services.WebMethod] 
public static bool saveMapData(string mapData) 

還要確保使用ScriptManager有EnablePageMethods="true"集。