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

时间:2012-11-01 11:00:59

标签: 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()));
            }
        }
    }
}

4 个答案:

答案 0 :(得分: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 {

只需做评论

中的建议

BTW,filterresults不应该是静态的

答案 1 :(得分:0)

获取您使用此代码的网络方法

$.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                
                   }

我认为这会对你有所帮助.....                    });

答案 2 :(得分:0)

如果您的脚本位于子文件夹中,则需要使用绝对路径:

url: "/filterresults.asmx/filterresults",

答案 3 :(得分:0)

如果您的脚本文件与filterresults.asmx文件位于同一文件夹中,那么您的网址是正常的,但如果没有,则需要编写正确的路径,例如下面的绝对路径:

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

您还需要将您的webservice定义为脚本服务以允许ajax调用,如下所示:

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

从您的网络方法中删除static关键字。 然后在您的网络方法上添加以下内容:

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

祝你好运!