2013-03-01 99 views
0

我必须对ASP.NET WebMethod进行ajax调用。为了做到这一点,我需要提供以下代码并测试ajaxcall是否可以通过将alert成功和错误的功能。我收到警报作为错误,无法找到我出错的地方。调用jQuery AJAX调用ASP.NET WebMethod时发生错误

这里是我的代码

<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> 

这里是我写的Multiple_Markers.aspx PAGE电泳的WebMethod的WebMethod工作完美。

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

我写的web服务实现了上述结果,但问题仍然存在。

[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(); 
} 
+0

控制台上的任何错误? – EnterJQ 2013-03-01 09:57:16

+1

控制台不应该出错,这是服务器端错误。 Kannan,你能指定服务器响应吗? – Sergio 2013-03-01 09:59:15

+0

我是jquery ajax的新手。你能指定'服务器响应' – Kannan 2013-03-01 10:44:24

回答

2

请确保您有以下行添加在你的web服务的WebMethod

[System.Web.Script.Services.ScriptService] 

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

还可以编辑这个网址:

url: "Multiple_Markers.aspx/BindAddress", 

相反Multiple_Markers.aspx页面,你应该写你的web服务的名称。 它可能会帮助你。

+0

我没有使用webservice.I在我的aspx页面中调用一个函数。是否需要编写我的aspx页面上面的代码 – Kannan 2013-03-01 10:41:36

+0

是的,有必要编写上面的代码来调用Ajax服务方法。你也可以把它写在你的aspx页面中。 – 2013-03-01 10:43:48

+0

我已经添加了webmethod上面的代码,但即时获取以下错误'属性'System.Web.Script.Services.ScriptService'对此声明类型无效。它仅对'class interface'声明有效' – Kannan 2013-03-01 10:54:52