WebService错误 500 内部错误 ASP.NET。

3

我已经在document ready函数中用JQuery编写了一个webservice调用,但它没有调用该函数。

以下是代码 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#代码 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;
}

1
它是在代码后台还是单独的网络服务中? - Mairaj Ahmad
3个回答

2
如果您的方法SliderBlock在代码后台中,则需要将此方法WebMethod化,以便通过Ajax调用。同时,您需要将其设置为静态方法,并使其能够通过GET请求调用,您需要在WebMethod启用GET请求。
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string SliderBlock(string section)
{
//Your code here
}

@naveen 谢谢。已更新。 - Mairaj Ahmad
@MairajAhmad明白了,我做出了必要的更改,但仍然出现500内部错误。 - bhavikshah28
你能否在浏览器控制台中检查详细的错误并在此处发布。 - Mairaj Ahmad
我有一个不同的页面,在该页面中我正在调用Web服务,而pgWebservice.aspx页面是不同的。控制台中的错误如下:GET http://localhost:1312/PMS/pgWebService.aspx/SliderBlock [HTTP/1.1 500 Internal Server Error 27ms] - bhavikshah28
请在控制台中点击链接以查看详情。 - Mairaj Ahmad

0

由于您的代码扩展名为.aspx,我假设您正在使用代码后置(页面方法)。因此,您需要对函数签名进行以下更改:

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

也就是说,

  1. 你的方法应该是静态的。
  2. 你的方法应该用 System.Web.Services.WebMethod 装饰。

而在你的 $.ajax 调用中,将 dataType 改为 json。

dataType: "json"

此外,请记住,pgWebService.aspx.cs中的PageMethid只能从pgWebService.aspx中调用。

我该如何从WebMethod调用TextBox的文本更改事件? - bhavikshah28

0

您的ajax请求仍然存在错误:

Content-Type: 当向服务器发送数据时,请使用此内容类型。但是,除了查询字符串参数之外,您没有向服务器发送数据,因为您正在执行GET操作。因此,如果您使用Web浏览器开发人员工具检查请求,则会看到一个带有此URL的GET请求:localhost/pgWebService.aspx/SliderBlock?section=selectedSection,因为...

Data: 要发送到服务器的数据。如果不是字符串,则将其转换为查询字符串。它将附加到GET请求的URL中。

dataType: 您从服务器返回的数据类型。但是在您的Web服务中,您返回的是带有HTML而不是JSON的字符串。


你能给一些例子吗? - bhavikshah28

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接