簡體   English   中英

JSON回發到c#webmethod添加文字控件

[英]JSON postback to c# webmethod add literal control

我正在學習webmethods並使用JSON發回給他們,我在下面有以下內容,但它說它無法找到webmethod(404)。 看不出我哪里出錯了,謝謝。

在頁面javascript:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
       <script type="text/javascript">
           $(document).ready(function () {
               $(".FilterResults").click(function () {
                   var topic = $(".DropDownList1").val();
                   var number = $(".DropDownList2").val();
                   var month = $(".DropDownList3").val();
                   $.ajax({
                       type: "POST",
                       url: "filterresultshold.asmx/filterresults",
                       data: "{'args': '" + topic + "'}",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function (msg) {
                           // If you return something from the method, it can be accessed via msg.d                
                       }
                   });

                   // To prevent the postback
                   return false;
               });
           });
</script> 

在ascx中:

<form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"><asp:Literal ID="Literal1" Text="Text to display" mode="PassThrough" runat="server" /></asp:PlaceHolder>
        <asp:DropDownList ID="DropDownList1" class="DropDownList1" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" class="DropDownList2" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" class="DropDownList3" runat="server"></asp:DropDownList>
        <asp:Button ID="FilterResults" class="FilterResults" runat="server" Text="Fill DropDownList" />
    </div>
</form>

在后面的代碼中:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for filterresults
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService {

    [System.Web.Services.WebMethod]
    public void filterresults(string args)
    {
        string[] data = args.Trim().Split(',');
        string topic = data[0];
        string number = data[1];
        string month = data[2];
        string control = "<umbraco:Macro alias='pdfarchivelist' runat='server' topic='" + topic + "' number='" + number + "' month='" + month + "'></umbraco:Macro>";
        //LiteralControl literal = new LiteralControl(control);
        //PlaceHolder PlaceHolder1 = new PlaceHolder();
        //PlaceHolder1.Controls.Add(literal);
    }

}

然后在后面的.ascx代碼中:

public partial class usercontrols_pdfarea : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {      
        if (!Page.IsPostBack)
        {
            // Populate Drops
            var rootNode = uQuery.GetRootNode();
            DropDownList1.Items.Add(new ListItem("SELEZIONA NUMERO"));
            DropDownList2.Items.Add(new ListItem("SELEZIONA MESE"));
            DropDownList3.Items.Add(new ListItem("SELEZIONA ARGOMENTO"));

            //display the password on the Gallery Folder in the media area
            var startMedia = uQuery.GetMediaByName("pdfs").FirstOrDefault();
            var DropList = rootNode.GetDescendantNodes().Where(x => x.NodeTypeAlias == "File");

            foreach (var item in startMedia.Children)
            {
                DropDownList1.Items.Add(new ListItem(item.getProperty("number").Value.ToString())); //NUMBER
                DropDownList2.Items.Add(new ListItem(item.getProperty("month").Value.ToString())); //MONTH
            }

            foreach (var item in startMedia.Children.Select(p => p.GetPropertyAsString("topic")).Distinct().ToList())
            {
                DropDownList3.Items.Add(new ListItem(item.ToString()));
            }
        }
    }
}
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService {

只需做評論中的建議

順便說一句, filterresults不應該是靜態的

要獲取您的Web方法,請使用此代碼

$.ajax({
                   type: "POST",
                   url: "Default.aspx/filterresults",
                   data: "{'args': '" + topic + "'}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (msg) {
                       // If you return something from the method, it can be accessed via msg.d                
                   }
               });

我認為你應該像這樣做一些改變

$.ajax({
                   type: "POST",
                   url: "Default.aspx/filterresults",
                   data: '{args:"' + topic + '"}',
                   contentType: "application/json; charset=utf-8",
                   dataType: 'json',
                   success: function (msg) {
                       // If you return something from the method, it can be accessed via msg.d                
                   }

我想這會對你有幫助.....));

如果您的腳本位於子文件夾中,則需要使用絕對路徑:

url: "/filterresults.asmx/filterresults",

如果您的腳本文件與filterresults.asmx文件位於同一文件夾中,那么您的網址是可以的,但如果沒有,則需要編寫正確的路徑,例如下面的絕對路徑:

url: "/services/filterresults.asmx/filterresults",

您還需要將Web服務定義為腳本服務以允許ajax調用,如下所示:

[System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService

並從您的webmethod中刪除static關鍵字。 然后在您的Web方法上添加以下內容:

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = 
    System.Web.Script.Services.ResponseFormat.Json)]
public string filterresults(string args)

最后確保web.config中的以下元素告訴webservices聽取Http Post調用:

<system.web>
<webServices>
  <protocols>
    <add name="HttpPost"/>
    <add name="HttpGet"/>
  </protocols>
</webServices>
</system.web>

祝好運!

暫無
暫無

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

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