Webservice error 500 Internal asp.net error
I wrote a webservice call in JQuery in the document ready function but did not call the function
Below is the JQuery code
`<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 # pgWebService code
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;
}
source to share
If your method SliderBlock
is in code lower than your method WebMethod
, that is the ajax call. Also you need to do it static
and call the requests GET
you need enable GET
on your WebMethod.
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string SliderBlock(string section)
{
//Your code here
}
source to share
Since your code is extensible .aspx
, I am assuming you are using 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)
I.e
- your method must be static.
- your method should be decorated
System.Web.Services.WebMethod
And in your $ .ajax call, change the dataType to json.
dataType: "json"
Also, remember that PageMethid in pgWebService.aspx.cs can only be called from pgWebService.aspx
source to share
You still have errors in the ajax request:
Content-Type . Use this content type when sending data to the server. But you are not sending data to the server other than the query string parameters, because you are doing a GET. So if you check with webbrowser developer tools the request you see GET with this url: localhost / pgWebService.aspx / SliderBlock? Section = selectedSection because ...
Data : data sent to the server. It is converted to a query string if it is not already a string. It is added to the url for GET requests.
dataType . The type of data you expect from the server. But in your webService, you are returning a string with HTML, not JSON.
source to share