3

I have written a webservice call in JQuery in document ready function but its not calling the function

Below is the code JQuery

`<script type="text/javascript">
$( document ).ready(function() {
var section = "Table - TLI (STOCK)";
$.ajax({
type: "GET",contentType: "application/json; charset=utf-8",
        url: "pgWebService.aspx/SliderBlock",
        dataType: "json",
        data: "{'section':'" + section + "'}",
        success: function (res) {
            //$("#Text1").val(res.text);
            console.log(res);
            alert("DONE");
        }
    });
});
</script>`

C# Code pgWebService

public static string SliderBlock(string section)
{
    string html = "<ul class='maketabs listing-table search-filter change-view'>";
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["TLI"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cn.Open();
    cmd.Connection = cn;
    cmd.CommandText = "Select * from CategoryDetails where section=" + section;
    SqlDataReader rs = cmd.ExecuteReader();
    while (rs.Read())
    {
        html="<li>"+rs.getValue(0).toString()+"</li>";
    }
    rs.Close();
    cmd.Dispose();
    cn.Close();
    html = html + "</ul>";
    return html;
}
bhavikshah28
  • 98
  • 1
  • 9

3 Answers3

2

If your method SliderBlock is in code behind than make your method WebMethod to be called by ajax.Also you need to make it static and to be called by GET you need to enable GET requests on your WebMethod.

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string SliderBlock(string section)
{
//Your code here
}
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

As your code has the extension .aspx I assume that you are using the code-behind ( Page Method ). So you need to make these changes to your function signature

[System.Web.Services.WebMethod]
public static string SliderBlock(string section)

That is,

  1. your method should be static.
  2. your method should be decorated with System.Web.Services.WebMethod

And in your $.ajax call, change the dataType to json.

dataType: "json"

Also, please bear in mind that the PageMethid in pgWebService.aspx.cs can be only called from pgWebService.aspx

naveen
  • 53,448
  • 46
  • 161
  • 251
0

You still have errors in the ajax request:

Content-Type: When sending data to the server, use this content type. But you are not sending data to the server except the query string parameters because you are doing a GET. So, if you inspect with webbrowser developer tools the request you see a GET with this URL: localhost/pgWebService.aspx/SliderBlock?section=selectedSection because...

Data: Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.

dataType: The type of data that you're expecting back from the server. But in your webService your are returning a string with HTML not JSON.

jlvaquero
  • 8,571
  • 1
  • 29
  • 45