Reputation: 829
I have to make a ajax call to an ASP.NET WebMethod.I've wrtiten the following code for doing that and tested whether ajaxcall is working properly by putting alert in both success and error function.I am getting alert as error and could not find where i have gone wrong.
Here's my code
<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>
Here's my webmethod written in Multiple_Markers.aspx page.The webmethod is working perfectly.
[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();
}
The webservice i have written to achieve the above result.But the problem still persists.
[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();
}
Upvotes: 0
Views: 1035
Reputation: 705
Make sure that you have added below line in your webservice before your webmethod
[System.Web.Script.Services.ScriptService]
I suggest you to create an ASMX file to use a webservice. It is easy to use. Create a webservice and Then make sure that you have add above line in your webservice before your webmethod and write your webmethod inside your webservice.
Also edit this url :
url: "Multiple_Markers.aspx/BindAddress",
Instead of Multiple_Markers.aspx page you should write your webservice name. It may help you.
Upvotes: 2