0

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();
}
4
  • Any error on console ? Commented Mar 1, 2013 at 9:57
  • 1
    Shouldn't be error on console, it's server-side error. Kannan, could you specify the server response? Commented Mar 1, 2013 at 9:59
  • I am new to jquery ajax.what do you meant by 'could you specify the server response' Commented Mar 1, 2013 at 10:44
  • 'could you specify the server response' means what is your web method is returning? Could you look into Net Tab of firefox for your request? Commented Mar 1, 2013 at 11:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

I am not using webservice.I am calling a function in my aspx page.Is it necessary to write the above codein my aspx page
Yes, It is necessary to write above code to call the Ajax service method. You can write it in your aspx page as well.
I have added the code above the webmethod,but im getting the following error 'Attribute 'System.Web.Script.Services.ScriptService'is not valid on this declaration type.It is valid on 'class interface' declarations only'
I have tried webservice.Now the function(BindAddress) is getting called(checked with breakpoint) and returning correctly.But the error still persists.Caould not identify where i have gone wrong.I have updated the questin with the webservice i have written.Kindly check it
Your method should be static. write public static OrganizationBAL[] BindAddress() instead of your method name.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.