BradAtCC
BradAtCC

Reputation: 41

Can you use jQuery ajax to retrieve data from Webservice?

I'm attempting to retrieve data from a SQL Server database, and using jQuery ajax to do it with. I need to bring this data into some jqPlot charts, and figured this was the best way to do that. If there is a better way, let me know.

Here is my current code:

Javascript/jQuery:

$(document).ready(function () {

$(".datepicker").datepicker();

$(".submitButton").click(handleSubmitButton);

});      //end of document ready

function handleSubmitButton() {

   var AnDParms = new Object();
   AnDParms.startDate = $('.start-date').val();
   AnDParms.endDate = $('.end-date').val();
   AnDParms.facility = $('.FacilityDDL').val();

   var DTO = { 'parameters': AndParms };

   $.ajax({
       type: "GET",
       contentType: "application/json; charset=utf-8",
       data: JSON.stringify(DTO),
       url: "GetAdmitsDischarges.asmx.cs/GetAandD",
       dataType: "json",
       success: function (data) {
           alert("Data: " + data.d);
           // do chart stuff here.
       },
       error: function (data) {
           alert("Error!" + data.d);
       } //end of success
   }); //end of ajax call
} //end of handleSubmitButton function

Web service (GetAdmitsDischarges.asmx.cs):

namespace DashboardTest2010
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class GetAdmitsDischarges : WebService
    {
        public class AnDParms
        {
            public string startDate { get; set; }
            public string endDate { get; set; }
            public string facility { get; set; }
        }

        public class AnDReturns
        {
            public string Admits { get; set; }
            public string Discharges { get; set; }
        }

        [WebMethod]
        public string GetAandD(AnDParms parameters)
        {
           //StringBuilder results = new StringBuilder();
           AnDReturns resultsObj = new AnDReturns();
           resultsObj.Admits = "0";
           resultsObj.Discharges = "0";
           string results = "";

           string connect = (the data connection string)
           StringBuilder query = new StringBuilder();
           query.Append("Select 'Admissions' SeriesType,");
           //the rest of the query.

           using (SqlConnection conn = new SqlConnection(connect))
           {
               using (SqlCommand cmd = new SqlCommand(query.ToString(), conn))
               {
                   cmd.Parameters.AddWithValue("StartDate", parameters.startDate.ToString());
                   cmd.Parameters.AddWithValue("EndDate", parameters.endDate.ToString());
                   cmd.Parameters.AddWithValue("FacilityID", parameters.facility.ToString());

                   SqlDataReader rdr = cmd.ExecuteReader();

                   if (rdr.HasRows)
                   {
                       while (rdr.Read())
                       {
                           if (rdr["SeriesType"].ToString() == "Admissions")
                           {
                               resultsObj.Admits = rdr["SeriesCount"].ToString();
                           }
                           if (rdr["SeriesType"].ToString() == "Discharges")
                           {
                               resultsObj.Discharges = rdr["SeriesCount"].ToString();
                           }
                       }
                       results = JsonConvert.SerializeObject(resultsObj);
                   }
               }
           }
           return results;
       }
   }
}

The mark up is just a simple page with 2 date fields (set to datepickers), 1 dropdown list (facility), and one submit button. Here are my script tags to make sure I have everything I need to have:

<script type="text/javascript" src="js/jqPlot/jquery.jqplot.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jqPlot/jquery.jqplot.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.3.custom.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/DashboardTest.js"></script>

The "jquery.js" is the jquery that came with jqPlot. I have to support IE 7+ (hence the json2.js) the DashboardTest.js is the javascript above.

Nothing happens when I run this, no alerts show up when I click the submit button. Any help would be much appreciated. My System Admin didn't allow me to install Fiddle, if you're wondering about that...

Upvotes: 0

Views: 2444

Answers (1)

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

While calling WebService, use GetAdmitsDischarges.asmx page for URL, not code-behind GetAdmitsDischarges.asmx.cs page.

Also 1 more thing. You're doing GET request, by default WebServices don't allow it. You may have to add

<webServices>
   <protocols>
      <add name="HttpGet"/>
   </protocols>
</webServices>

to your web.config.

Upvotes: 2

Related Questions