c# - JSON 回发到 c# webmethod 添加文字控制

标签 c# asp.net json webmethod

我正在学习 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 不应该是静态的

关于c# - JSON 回发到 c# webmethod 添加文字控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13175652/

相关文章:

c# - 拦截 Ninject 实例激活?

c# - 1/C# 中的大整数

javascript - ASP.NET- args.Value 的类型是什么

javascript - 如何从分配的隐藏字段值中获取文本框值

java - 如何使用 HttpURLConnection 在 Android 中的 url 末尾添加 json 数组?

javascript - 检查字符串是否在 json 对象中

c# - "$"美元字符前缀 cookie 名称

c# - fileupload控件在updatepanel下时如何上传图片?

javascript - 如何在 html 元素的 jquery 中使用 Ajax 从 Json 发送数据?

C# 当我在列表框中选择一个项目时,我应该使用哪个事件在文本框中显示数据?