簡體   English   中英

webservice錯誤500內部錯誤asp.net

[英]webservice error 500 Internal Error asp.net

我已經在文檔就緒函數中用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;
}

如果你的方法SliderBlock在代碼后面而不是讓你的方法WebMethod被ajax調用。你需要使它成為static並由GET調用你需要在你的WebMethod上enable GET請求。

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string SliderBlock(string section)
{
//Your code here
}

由於您的代碼具有擴展名.aspx我假設您使用的是代碼隱藏(Page Method)。 因此,您需要對功能簽名進行這些更改

[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調用

您仍然在ajax請求中有錯誤:

Content-Type :將數據發送到服務器時,請使用此內容類型。 但是,除了查詢字符串參數之外,您不會向服務器發送數據,因為您正在進行GET。 因此,如果您使用webbrowser開發人員工具檢查請求,您會看到使用此URL的GET: localhost / pgWebService.aspx / SliderBlock?section = selectedSection,因為......

數據 :要發送到服務器的數據。 如果不是字符串,它將轉換為查詢字符串。 它附加到GET請求的URL。

dataType :您期望從服務器返回的數據類型。 但是在你的webService中,你返回一個HTML而不是JSON的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM