我必须对 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>
这是我在 Multiple_Markers.aspx 页面中编写的 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();
}
我为实现上述结果而编写的网络服务。但问题仍然存在。
[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();
}