我必须对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();
}
控制台上的任何错误? – EnterJQ 2013-03-01 09:57:16
控制台不应该出错,这是服务器端错误。 Kannan,你能指定服务器响应吗? – Sergio 2013-03-01 09:59:15
我是jquery ajax的新手。你能指定'服务器响应' – Kannan 2013-03-01 10:44:24