使用jquery从javascript调用aspx服务器方法的问题

时间:2011-05-23 13:23:52

标签: asp.net jquery

嗨,我试图用jquery从javascript中调用代码中的方法,但它不起作用所以我需要帮助,因为。

这是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();
        }

    }

感谢您的帮助

4 个答案:

答案 0 :(得分:2)

页面上的Web方法必须是静态的:

变化:

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

要:

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

还要确保ScriptManager设置了EnablePageMethods="true"

答案 1 :(得分:1)

我通常使用.stringify() include的JSON2.js方法来确保我的JSON是正确的,但您可能会尝试将数据行更改为:

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

答案 2 :(得分:0)

尝试使用ASP.NET javascript函数调用它,它们由ASP.NET运行时自动创建,对我来说效果很好

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

答案 3 :(得分: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数据没有问题。但是,无论如何都要感谢