jquery - ASMX Web 方法调用一次
问题描述
我在 asmx 页面中有一个 web 方法。我从 jquery Ajax 调用它。一切正常,只需 1 次,然后永远不会调用 web 方法。
[WebMethod(EnableSession = true)]
public static string WsCom(AjaxObjectsHelper.UpdatePageAjax Param)
{
try
{
if (HttpContext.Current.Session["AppName"] != null && HttpContext.Current.Session["USR"] != null)
{
string xRes = "";
if (Param.Action == "_GMH")
{
xRes = "";
}
else if (Param.Action == "_GMHL")
{
xRes = "";
}
else
{
xRes = SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax("Invalid Action.", false));
}
return xRes;
}
else
{
return SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax("Unauthorized access", false));
}
}
catch (Exception ex)
{
return SiteHelper.AjaxObjectsHelper.GetJson(new SiteHelper.AjaxObjectsHelper.ErrorAjax(ex.Message, false));
}
}
这是我的ajax调用:
function (xF, xCB) {
var callData = { Param: { Action: "_GMH", Data: xF } }
$.ajax({
type: "POST",
url: "/mypage.asmx/WsCom"
data: JSON.stringify(callData),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (response) {
console.log("success")
}
}).fail(function (jqXHR, textStatus, error) {
console.log(jqXHR.responseText);
});
}
每次都调用ajax成功,而不是asp.net webmethod。(它只是第一次断点)有什么想法吗?它看起来像一个缓存问题。
解决方案
要允许使用 ASP.NET AJAX 从脚本调用 Web 服务,请在命名空间列表之后添加以下行。
[System.Web.Script.Services.ScriptService]
此外,将以下行添加到您的方法中。
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string WsCom(AjaxObjectsHelper.UpdatePageAjax Param)
{
推荐阅读
- java - 收到此错误:“无法初始化类 org.apache.logging.log4j.LogManager”
- typescript - 如何检测 Angular8 服务中对象的变化?
- php - TYPO3:使用构造函数将服务类注入 AuthServiceClass
- ios - 如何使用 alamofire 以 json 的顺序快速发布请求?
- asp.net - Json 序列化器引用处理 asp.net
- drop-down-menu - Selenium IDE 下拉菜单选择
- sql - 嵌套游标循环
- yii2 - Yii2 将控制器的所有动作重定向到一个
- javascript - 重定向返回后如何访问 IFRAME 中的查询字符串参数
- selenium - Chrome 的 useAutomationExtension 功能是什么?