我在jQuery的ajax调用中遇到错误.
这是我的jQuery函数:
function DeleteItem(RecordId, UId, XmlName, ItemType, UserProfileId) {
var obj = {
RecordId: RecordId,
UserId: UId,
UserProfileId: UserProfileId,
ItemType: ItemType,
FileName: XmlName
};
var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);
$.ajax({
type: "POST",
url: "EditUserProfile.aspx/DeleteRecord",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
if (msg.d != null) {
RefreshData(ItemType, msg.d);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error occured during deleting");
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的WebMethod:
[WebMethod]
public static string DeleteRecord(Int64 RecordId, Int64 UserId, Int64 …Run Code Online (Sandbox Code Playgroud) 被困这几个小时了
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
Run Code Online (Sandbox Code Playgroud)
我试图在我的ASP.Net Webform中调用这个WebMethod
[WebMethod]
public static string GetClients(string searchTerm, int pageIndex)
{
string query = "[GetClients_Pager]";
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
return GetData(cmd, pageIndex).GetXml();
}
Run Code Online (Sandbox Code Playgroud)
从这个jquery.ajax
function GetClients(pageIndex) {
$.ajax({
type: "POST",
url: "ConsultaPedidos.aspx/GetClients",
data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) { …Run Code Online (Sandbox Code Playgroud) 是否可以将WebMethod放在ascx.cs文件中(对于UserControl),然后从客户端jQuery代码调用它?
出于某些原因,我无法将WebMethod代码放在.asmx或.aspx文件中.
示例:在ArticleList.ascx.cs中,我有以下代码:
[WebMethod]
public static string HelloWorld()
{
return "helloWorld";
}
Run Code Online (Sandbox Code Playgroud)
在ArticleList.ascx文件中,我可以调用WebMethod,如下所示:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataFilter: function(data)//makes it work with 2.0 or 3.5 .net
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "ArticleList.ascx/HelloWorld",
success: function(msg) {
alert(msg);
}
});
Run Code Online (Sandbox Code Playgroud)
而firebug的错误是:
<html>
<head>
<title>This type of page is not served.</title> …Run Code Online (Sandbox Code Playgroud) 我正在使用使用Ajax(json)/ Webmethod函数的页面测试Azure服务器.
其中一些函数HttpContext.Current.User.Identity.IsAuthenticated在运行代码之前检查.不幸的是,如果用户登录并且页面没有向服务器发出完整的回发请求,那么只有那些检查HttpContext.Current.User.Identity.IsAuthenticated在几分钟后完全停止运行的webmethods函数没有给出任何错误.他们甚至没有运行else代码块(见下文).
我已经在本地服务器上测试了这些页面,即使在长时间不活动之后,一切正常.这是一个webmethod的例子
[WebMethod]
public static string serviceMenu(int IDservice)
{
StringBuilder SBphotoMenu = new StringBuilder();
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// Do stuff
}
else
{
// Do other stuff
}
return SBphotoMenu.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我正在调用webmethod如下:
function serviceMenu(IDservice) {
$.ajax({
type: "POST",
url: "/UserControls/serviceMenu",
data: "{ IDservice: " + IDservice }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Do Stuff
}
})
}
Run Code Online (Sandbox Code Playgroud)
仅当用户登录时才会出现此问题.现在,如果用户未登录,则即使在Azure上,所有功能也能正常工作.
事实上,当webmethods停止运行并且我刷新页面时,用户仍然登录并且webmethods再次开始运行但只有几分钟,然后再次发生相同的行为.
出了什么问题?
我正在通过jQuery的ajaxpost方法通过web方法加载标签内容数据,大约有200-300条记录.并在控制台中收到以下错误:
错误:Sys.Net.WebServiceFailedException:Sys.Net.WebServiceFailedException:System.InvalidOperationException--使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
maxJsonLength像这样在Web.config中更改属性的长度没有帮助.
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644" />
</webServices>
</scripting>
</system.web.extensions>
</configuration>
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决这个问题?
我在下面设置了一个断点,WebMethod但我从来没有打过断点.
CS:
[WebMethod]
public static string search()
{
return "worked";
}
Run Code Online (Sandbox Code Playgroud)
ASPX:
function search() {
$.ajax({
type: "POST",
url: "ProcessAudit/req_brws.aspx/search",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg)
}
});
}
Run Code Online (Sandbox Code Playgroud)
<button id = "btnSearch" onclick = "search()" >Search</button>
Run Code Online (Sandbox Code Playgroud) 如何使用javascript(aspx)....从客户端调用服务器端(aspx.cs)中的非静态方法....?
据我所知,我可以从客户端调用服务器端的静态方法...
服务器端:
[WebMethod]
public static void method1()
{
}
Run Code Online (Sandbox Code Playgroud)
客户端:
<script language="JavaScript">
function keyUP()
{
PageMethods.method1();
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)
有用.现在如何从客户端调用非静态方法?
我有一个ajax请求使用"POST"很好,但当使用"GET"它给我以下错误,
{"Message":"An attempt was made to call the method \u0027GetSomething\u0027
using a GET request, which is not allowed.","StackTrace":" at
System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData
methodData, HttpContext context)\r\n at
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,
WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
Run Code Online (Sandbox Code Playgroud)
所以这是我的代码,在客户端,
function test() {
$.ajax({
url: "Default4.aspx/GetSomething",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) { debugger; alert(res.d); },
error: function (res) { debugger; alert("error"); }
});
}
Run Code Online (Sandbox Code Playgroud)
在服务器端,
[WebMethod]
public static string GetSomething()
{
return "got something";
}
Run Code Online (Sandbox Code Playgroud)
使用"GET"时我得到错误的任何原因??
我们有一个较旧的ASP.NET WebForms应用程序,它通过$.ajax()在客户端使用jQuery 调用来执行AJAX请求,在使用[WebMethod]属性修饰的页面代码隐藏中调用静态方法.
如果在WebMethod中发生未处理的异常,则它不会触发Application_Error事件,因此我们的错误记录器(ELMAH)不会将其捕获.这是众所周知的而不是问题 - 我们将所有WebMethod代码包装在try-catch块中,并将异常手动记录到ELMAH.
但是,有一个案例令我难过.如果格式错误的Json发布到WebMethod URL,它会在输入我们的代码之前抛出异常,我找不到任何方法来捕获它.
例如,这个WebMethod签名
[WebMethod]
public static string LeWebMethod(string stringParam, int intParam)
Run Code Online (Sandbox Code Playgroud)
通常使用Json有效负载调用,如:
{"stringParam":"oh hai","intParam":37}
Run Code Online (Sandbox Code Playgroud)
我尝试使用Fiddler进行测试,将有效负载编辑为格式错误的Json:
{"stringParam":"oh hai","intPara
Run Code Online (Sandbox Code Playgroud)
并ArgumentException从JavaScriptObjectDeserializer发送到客户端获得以下错误响应(这是在本地运行的简单测试应用程序,没有自定义错误):
{"Message":"Unterminated string passed in. (32): {\"stringParam\":\"oh hai\",\"intPara","StackTrace":" at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()\r\n at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeMemberName()\r\n at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at
System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at …Run Code Online (Sandbox Code Playgroud) 我想使用以下代码在asp.net c#application中调用web方法
jQuery的:
jQuery.ajax({
url: 'AddToCart.aspx/AddTo_Cart',
type: "POST",
data: "{'quantity' : " + total_qty + ",'itemId':" + itemId + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function () {
alert("Start!!! ");
},
success: function (data) {
alert("a");
},
failure: function (msg) { alert("Sorry!!! "); }
});
Run Code Online (Sandbox Code Playgroud)
C#代码:
[System.Web.Services.WebMethod]
public static string AddTo_Cart(int quantity, int itemId)
{
SpiritsShared.ShoppingCart.AddItem(itemId, quantity);
return "Add";
}
Run Code Online (Sandbox Code Playgroud)
但它总是调用page_load.我该如何解决?