调用ASP.NET WebMethod的jQuery AJAX调用时出错

时间:2013-03-01 09:55:07

标签: jquery asp.net ajax

我必须对ASP.NET WebMethod进行ajax调用。我已经使用以下代码执行此操作并通过在成功和错误函数中设置警报来测试ajaxcall是否正常工作。我收到警告为错误并且无法找到我出错的地方。

这是我的代码

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Multiple_Markers.aspx/BindAddress",
        data: "{}",
        dataType: "json",
        success: function(data) {
           alert("success")
        },
        error: function(result) {
            alert("Error");
        }
    });
});
</script>

这是我在multi_Markers.aspx页面上编写的web方法.Web方法运行良好。

[WebMethod]
public static OrganizationBAL[] BindAddress()
{
    DataTable dt = new DataTable();
    List<OrganizationBAL> lstAddress = new List<OrganizationBAL>();
    ProgramDAL clsPgmDAL=new ProgramDAL();

    using (SqlConnection sqlcon = new SqlConnection(clsPgmDAL.connStr))
    {
        string str="select street_address as title,latitude as lat,longitude as lng,"+
                   "street_address+','+city+','+state+','+country as descritpion from dbo.tblOrganization_Address";
        using (SqlCommand cmd = new SqlCommand(str, sqlcon))
        {
            sqlcon.Open();
            SqlDataAdapter sqlad = new SqlDataAdapter(cmd);
            sqlad.Fill(dt);
            foreach (DataRow dRow in dt.Rows)
            {
                OrganizationBAL clsOrg = new OrganizationBAL();
                clsOrg.CITY = dRow["title"].ToString();
                clsOrg.LATITUDE = dRow["lat"].ToString();
                clsOrg.LONGITUDE = dRow["lng"].ToString();
                clsOrg.ADDRESS_TYPE_LOCATION = dRow["descritpion"].ToString();
                lstAddress.Add(clsOrg);
            }
        }
    }
    return lstAddress.ToArray();
}

我写的网络服务是为了达到上述结果。但问题仍然存在。

    [WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public OrganizationBAL[] BindAddress()
{
    DataTable dt = new DataTable();
    List<OrganizationBAL> lstAddress = new List<OrganizationBAL>();
    ProgramDAL clsPgmDAL = new ProgramDAL();

    using (SqlConnection sqlcon = new SqlConnection(clsPgmDAL.connStr))
    {
        string str = "select street_address as title,latitude as lat,longitude as lng," +
                   "street_address+','+city+','+state+','+country as descritpion from dbo.tblOrganization_Address";
        using (SqlCommand cmd = new SqlCommand(str, sqlcon))
        {
            sqlcon.Open();
            SqlDataAdapter sqlad = new SqlDataAdapter(cmd);
            sqlad.Fill(dt);
            foreach (DataRow dRow in dt.Rows)
            {
                OrganizationBAL clsOrg = new OrganizationBAL();
                clsOrg.CITY = dRow["title"].ToString();
                clsOrg.LATITUDE = dRow["lat"].ToString();
                clsOrg.LONGITUDE = dRow["lng"].ToString();
                clsOrg.ADDRESS_TYPE_LOCATION = dRow["descritpion"].ToString();
                lstAddress.Add(clsOrg);
            }
        }
    }
    return lstAddress.ToArray();
}

1 个答案:

答案 0 :(得分:2)

确保在您的网络服务

之前已在网络服务中添加以下行
[System.Web.Script.Services.ScriptService]

我建议您创建一个ASMX文件以使用Web服务。它很容易使用。创建一个web服务,然后确保在webmethod之前在webservice中添加以上行,并将webmethod写入web服务。

同时编辑此网址:

url: "Multiple_Markers.aspx/BindAddress",

您应该编写您的Web服务名称,而不是Multiple_Markers.aspx页面。 它可能对你有帮助。