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

我正在学习webmethods并使用JSON发回给他们,我在下面有以下内容,但它说它无法找到webmethod(404)。 不知道我哪里出错了,谢谢。

在页面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; }); });  

在ascx中:

 

在后面的代码中:

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; ///  /// Summary description for filterresults ///  [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 = ""; //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是可以的,但如果没有,则需要编写正确的路径,例如下面的绝对路径:

 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调用:

         

祝好运!