我在我的页面的代码隐藏文件中创建了一个WebMethod,如下所示:
[System.Web.Services.WebMethod()]
public static string Test()
{
return "TEST";
}
Run Code Online (Sandbox Code Playgroud)
我创建了以下HTML页面来测试它:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
<script type="text/javascript">
function test() {
$.ajax({
type: "POST",
url: "http://localhost/TestApp/TestPage.aspx/Test",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(msg) {
alert(msg.d);
}
});
}
</script>
</head>
<body>
<button onclick="test();">Click Me</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,AJAX会触发,但不会返回任何内容.当我调试我的代码时,该方法Test()甚至不会被调用.有任何想法吗?
我有两个web方法,我希望重载:
<WebMethod()> _
Public Function GetProject(ByVal id As Int32) As Project
<WebMethod(MessageName:="GetProjects")> _
Public Function GetProject(ByVal filter As String) As Projects
Run Code Online (Sandbox Code Playgroud)
我读到了使用MessageName重载,但是我无法让它工作.这可能吗?
我是jqGrid的新手,我发现很难按照文档jqGrid文档
在设置JQGrid时,我无法弄清楚如何调用WebMethod.我成功地进行了Ajax调用以获取数据,然后使用本地数据设置JQGrid.我认为这是设置过程中的一个额外步骤,我应该能够使用url属性提供webmethod的路径.
editurl属性是相同的方式.我从来没有真正收到邮件到服务器.
原始代码
尝试JQGrid设置
function GetData()
{
$('#list').jqGrid({
type: "POST",
url: "Default.aspx/GetUsersJSON",
datatype: "json",
height: 250,
colName: ['Username', 'Email'],
colModel: [
...
}).jqGrid(
'navGrid',
'#pager',
{
edit: true,
add: true,
del: true
});
}
Run Code Online (Sandbox Code Playgroud)
的WebMethod
[WebMethod]
public static string GetUsersJSON()
{
var users = new List();
using(UserAdministrationSandboxDataContext uasd = new UserAdministrationSandboxDataContext())
{
users = uasd.GetUserList();
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(users);
}
Run Code Online (Sandbox Code Playgroud)
现行守则
我现在正常工作,但我还有一个最后的问题.为什么我必须设置'repeatitems:false'才能显示内容?
要使其工作的一些注意事项包括设置ajax请求的不同方法.
(Ajax:type)是(jqgrid:mtype)(Ajax:contentType)是(jqgrid:ajaxGridOptions:{contentType:})
最后,从文档中了解如何设置JSONReader的文档.
希望这有助于其他人并感谢Oleg的所有帮助.
JS
function GetUserDataFromServer()
{
$('#list').jqGrid({
url: "Default.aspx/GetUsersJSON",
mtype: …Run Code Online (Sandbox Code Playgroud) 我通过jquery.ajax将包含字符串的javascript变量传递给服务器.虽然调用了"成功"条件,但从不调用服务器端WebMethod.客户:
$.ajax({
type: "post",
url: "Playground.aspx/childBind",
data: {sendData: ID},
//contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (result) { alert("successful!" + result.d); }
})
Run Code Online (Sandbox Code Playgroud)
服务器:
[WebMethod]
public static string childBind(string sendData)
{
return String.Format("Hello");
}
Run Code Online (Sandbox Code Playgroud) 这是一些javascript:
$.ajax({
type: "POST",
url: "default.aspx/GetDate",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "json",
success: function(result) {
alert(result.d);
}
});
Run Code Online (Sandbox Code Playgroud)
上面的方法可以正常工作,并在default.aspx中警告从[WebMethod]返回的名为GetDate的字符串
但是当我使用时:
$.post(
"default.aspx/GetDate",
{},
function(result) {
alert(result.d);
},
"json"
);
Run Code Online (Sandbox Code Playgroud)
此成功方法中的警报永远不会触发.
在firebug中我可以看到POST基本上有效 - 它返回200 OK
但是在这种情况下的响应是整个default.aspx页面的HTML而不是我使用$ .ajax()方法时返回的JSON.
编辑:
firebug中显示的响应和请求标头不相同.
使用$ .ajax()......
REQUEST:
Accept application/json, text/javascript, */*; q=0.01
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding gzip, deflate
Accept-Language en-gb,en;q=0.5
Connection keep-alive
Content-Type application/json; charset=utf-8
Cookie (removed)
Host (removed)
Referer (removed)
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
X-Requested-With XMLHttpRequest
RESPONSE:
Cache-Control private, max-age=0 …Run Code Online (Sandbox Code Playgroud) 因此,当我在web.config中设置maxJsonLength时,我不断在我的.net站点上获得500 - 内部服务器错误页面.
我正在修改.config,因为即使我在我的vb.net JavaScriptSerializer上使用MaxJsonLength = Int32.MaxValue,我仍然会收到一个大字典的InvalidOperationException,我试图传输,即使它远低于4GB MaxJsonLength @ Int32.MaxValue允许或甚至假设的4mb默认限制.
如果这意味着什么,我正在使用toolkitscriptmanager.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"/>
</webServices>
</scripting>
Run Code Online (Sandbox Code Playgroud)
这没有帮助(实际上,它没有上面的代码也给出500错误)
<sectionGroup name="system.web.extensions" type="System.Web.Extensions">
<sectionGroup name="scripting" type="System.Web.Extensions">
<sectionGroup name="webServices" type="System.Web.Extensions">
<section name="jsonSerialization" type="System.Web.Extensions"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
Run Code Online (Sandbox Code Playgroud)
听说这应该有助于InvalidOperationException,但它没有.我拿出来了,仍然是500错误.
<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
Run Code Online (Sandbox Code Playgroud)
提前谢谢了!
编辑
同样的问题,但他的解决方案对我不起作用.他添加的最后一个代码也给出了500错误. 升级到.NET 4.0时出现<system.web.extensions>配置组问题
我有一个控件,它有一个文本框,当它的内容发生变化时,会使这个javascript函数变得棘手:
page参数是document.URL因为控件没有附加.asxc页面,并且fieldValue是文本框的值.
function UpdateFieldsOnListSelection(page, fieldValue) {
$.ajax({
type: "POST",
url: page + "/IsSelectedListPictureLibrary",
data: { "libraryInfo": fieldValue },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("Success!");
},
error: function (jqXHR, textStatus, errorThrown) {
alert("jqXHR: " + jqXHR.status + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown);
}
});
};
Run Code Online (Sandbox Code Playgroud)
它不断抛出这个错误:
jqXHR:200
textStatus:parsererror
errorThrown:SyntaxError:JSON.parse:意外字符
代码IsSelectedListPictureLibrary:
[WebMethod]
public static bool IsSelectedListPictureLibrary(string libraryInfo)
{
if (string.IsNullOrEmpty(libraryInfo)) return false;
var common = new Utility(); …Run Code Online (Sandbox Code Playgroud) 我调用了一个WebMethod通过Fiddler,我在"请求体"中提供了2个需要的参数,我得到一个奇怪的行为:
HttpContext.Current.Request.FormWCF在调试时WebMethod,POST参数不会传输到service => ,HttpContext.Current.Request.Form为空.任何线索为什么会这样?
这是我的代码:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/getsomething")]
[FaultContract(typeof(ResponseMessageStatus))]
[DynamicResponseType]
public Stream GetSomething()
{
var par1 = HttpContext.Current.Request.Form["myparameter"] ;
//after 10 requests, HttpContext.Current.Request.Form is empty.
...
}
Run Code Online (Sandbox Code Playgroud) 我的* .aspx站点中有大约50多个AJAX WebMethod,它由JQuery调用(有时是原始的jquery,有时是在lib的基础上)。
这里有一些例子:
[WebMethod]
public static string GetLog()
{
DAL.LogService log = new DAL.LogService();
string items = log.GetLog();
return items;
}
[WebMethod]
public static void ClearBenchmarks()
{
DAL.LogService log = new DAL.LogService();
log.ClearBenchmarks();
}
[WebMethod]
public static void WriteLog(string message1, string message2, string user, string type)
{
DAL.LogService.WriteLog(message1, message2, user, type);
}
Run Code Online (Sandbox Code Playgroud)
实际上,在大多数时候,它们仅重定向到我的应用程序的dataLayer,但是实际上,ofc,我有50多个单一方法...
现在,我想在我的应用程序中包括错误处理-如何在不尝试捕获每种方法的情况下进行全局处理?
我正在尝试使用SSIS中的Web服务任务调用Web服务.在HTTP连接管理器中我给了服务器URL,我还没有定义任何代理服务器.我下载了WSDL文件.我在"输入"选项卡中选择了"服务和方法".该方法需要我传递的字符串参数.我收到以下错误.我甚至尝试将保护级别更改为DontSaveSensitive但仍然出现此错误.请帮忙
Error: 0xC002F304 at Web Service Task, Web Service Task: An error
occurred with the following error message:
"Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebserviceTaskException:
Could not execute the Web method. The error is: Could not load file or
assembly 'Microsoft.SqlServer.WebServiceTask, Version=14.100.0.0,
Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its
dependencies. The system cannot find the file specified.at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTaskUtil.Invoke(DTSWebMethodInfo methodInfo, String serviceName, Object connection, VariableDispenser taskVariableDispenser)
at Microsoft.SqlServer.Dts.Tasks.WebServiceTask.WebServiceTask.executeThread()".
Run Code Online (Sandbox Code Playgroud) webmethod ×10
jquery ×6
.net ×3
ajax ×3
asp.net ×3
c# ×2
web-services ×2
asp.net-ajax ×1
jqgrid ×1
json ×1
overloading ×1
post ×1
ssis ×1
wcf ×1
web-config ×1