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

JSON回发到c#webmethod添加文字控件,c#,asp.net,json,webmethod,C#,Asp.net,Json,Webmethod,我正在学习webmethods并使用JSON发回给他们,我有以下内容,但它说找不到webmethod(404)。我看不出我错在哪里了,谢谢 在javascript页面中: $(文档).ready(函数(){ $(“.FilterResults”)。单击(函数(){ var topic=$(“.DropDownList1”).val(); 变量编号=$(“.DropDownList2”).val(); var month=$(“.DropDownList3”).val(); $.ajax({ 类

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

在javascript页面中:


$(文档).ready(函数(){
$(“.FilterResults”)。单击(函数(){
var topic=$(“.DropDownList1”).val();
变量编号=$(“.DropDownList2”).val();
var month=$(“.DropDownList3”).val();
$.ajax({
类型:“POST”,
url:“filterresultshold.asmx/filterresults”,
数据:“{args':'”+topic+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(msg){
//如果您从该方法返回某些内容,则可以通过msg.d访问它
}
});
//防止回发
返回false;
});
});
在ascx中:


在代码隐藏中:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
/// 
///filterresults的摘要说明
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类filterresultshold:System.Web.Services.WebService{
[System.Web.Services.WebMethod]
公共无效筛选器结果(字符串参数)
{
string[]data=args.Trim().Split(',');
字符串主题=数据[0];
字符串编号=数据[1];
字符串月份=数据[2];
字符串控件=”;
//LiteralControl literal=新的LiteralControl(control);
//占位符占位符1=新占位符();
//占位符1.Controls.Add(文字);
}
}
然后在.ascx代码后面:

public部分类usercontrols\u pdfarea:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{      
如果(!Page.IsPostBack)
{
//填充水滴
var rootNode=uQuery.GetRootNode();
DropDownList1.Items.Add(新列表项(“selezionanumero”);
DropDownList2.Items.Add(新列表项(“SELEZIONA-MESE”));
DropDownList3.Items.Add(新列表项(“selezionaargomento”));
//在媒体区域的Gallery文件夹中显示密码
var startMedia=uQuery.GetMediaByName(“PDF”).FirstOrDefault();
var DropList=rootNode.getDegenantNodes()。其中(x=>x.NodeTypeAlias==“文件”);
foreach(startMedia.Children中的变量项)
{
DropDownList1.Items.Add(新的ListItem(item.getProperty(“number”).Value.ToString());//number
DropDownList2.Items.Add(新列表项(item.getProperty(“month”).Value.ToString());//month
}
foreach(startMedia.Children.Select(p=>p.GetPropertyAsString(“主题”)).Distinct().ToList()中的变量项)
{
DropDownList3.Items.Add(新列表项(item.ToString());
}
}
}
}

要获取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                
                   }
我想这会帮助你。。。。。 });

按照评论中的建议去做就行了


顺便说一句,
filterresults
不应该是静态的

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

url: "/filterresults.asmx/filterresults",

如果脚本文件与filterresults.asmx文件位于同一文件夹中,则url是可以的,但如果不是,则需要写入正确的路径,例如如下所示的绝对路径:

url: "/services/filterresults.asmx/filterresults",
[System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService
您还需要将Web服务定义为允许ajax调用的脚本服务,如下所示:

url: "/services/filterresults.asmx/filterresults",
[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)
最后确保在告诉webservices侦听Http Post调用的部分中的web.config中有以下元素:

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


祝你好运

您是通过服务添加控件吗?您的filterresults方法是用ascx代码编写的吗?我认为您需要在您的aspx代码中编写web方法。您是否能够通过浏览URL获得响应-错误或其他?我已将其更新为使用webservice,并将webmethod代码放置在webservice文件中,并更新javascript以指向它,但得到500个内部服务错误。谢谢,越来越近,我仍然收到一个500内部服务器错误,我认为它可能没有正确地传递dropdownlist值,我尝试了var topic=$(“#”).val();但是我得到一个“名称'DropDownList1'在当前上下文中不存在”。如果您在Firefox上安装了FireBug,或者如果您使用Chrome,请按F12,然后在Net-->XHR-->Post下查看发送了什么请求。谢谢,我得到一个无法创建类型'filterresults'。在System.Web.UI.SimpleWebHandlerParser.GetType(String typeName)错误中,您是否取消了webservice类顶部的
[System.Web.Script.Services.ScriptService]
注释?您是否必须在Web.config或ascx的scriptmanager中声明webmethod服务?