Javascript 将变量从jQuery传递到WebMethod(身份验证失败)
我在VS2013中创建了一个新的WebForms应用程序。我没有改变任何东西,而是创建了一个简单的页面。我想在用户单击按钮时在客户端加载此表Javascript 将变量从jQuery传递到WebMethod(身份验证失败),javascript,jquery,asp.net,ajax,webmethod,Javascript,Jquery,Asp.net,Ajax,Webmethod,我在VS2013中创建了一个新的WebForms应用程序。我没有改变任何东西,而是创建了一个简单的页面。我想在用户单击按钮时在客户端加载此表 <table id="tblHelpRow"> <thead> <tr class="title"> <th>F2 </th> <th>F3
<table id="tblHelpRow">
<thead>
<tr class="title">
<th>F2
</th>
<th>F3
</th>
</tr>
</thead>
<tbody id="helpRowBody">
<%=MyRow %>
</tbody>
</table>
<asp:LinkButton ID="lnkEdit" runat="server" onclick="fnEdit();" />
我的代码中有一个WebMethod,其存储结果是公共属性。我存储在会话中的数据源,但我需要从jquery传递rowID
[WebMethod]
public static string GetRow(int rowID)
{
DataTable dt = (DataTable)HttpContext.Current.Session["SourceData"];
MyRow = "<tr>" +
"<td>" + dt.Rows[rowID]["F2"].ToString() + "</td>" +
"<td>" + dt.Rows[rowID]["F3"].ToString() + "</td>" +
"</tr>";
return "done";
}
[WebMethod]
公共静态字符串GetRow(int rowID)
{
DataTable dt=(DataTable)HttpContext.Current.Session[“SourceData”];
MyRow=“”+
“”+dt.Rows[rowID][“F2”].ToString()+“”+
“”+dt.Rows[rowID][“F3”].ToString()+“”+
"";
返回“完成”;
}
但我没有得到任何结果。当我将breakpont设置为成功时,我得到了“身份验证失败”错误,并且没有执行此webmethod。有什么问题吗?我没有更改ant身份验证设置。请尝试删除ScriptMethod属性。您正在指定POST操作类型,但我相信ScriptMethod强制请求在默认情况下为GET。此外,我认为您的参数需要是JSON字符串,而不仅仅是整数:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});
尝试删除ScriptMethod属性。您正在指定POST操作类型,但我相信ScriptMethod强制请求在默认情况下为GET。此外,我认为您的参数需要是JSON字符串,而不仅仅是整数:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});
尝试删除ScriptMethod属性。您正在指定POST操作类型,但我相信ScriptMethod强制请求在默认情况下为GET。此外,我认为您的参数需要是JSON字符串,而不仅仅是整数:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});
尝试删除ScriptMethod属性。您正在指定POST操作类型,但我相信ScriptMethod强制请求在默认情况下为GET。此外,我认为您的参数需要是JSON字符串,而不仅仅是整数:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});
在我的VS2013 web表单项目中,罪魁祸首是:
var settings=newfriendlyurlsettings{AutoRedirectMode=RedirectMode.Permanent}代码>
使用FriendlyUrls的默认设置解决了这个问题--不要使用RedirectMode.Permanent
像这样的ajax调用,其中数据参数很复杂
$.ajax({
type: "POST",
contentType: "application/json",
url: applicationBaseUrl + "mypage.aspx/Calc",
data: params // data is json
}).success(function (data, status, xhr) {
//...
}).error(function (xhr, status, error) {
//...
});
WebMethod是这样的
[WebMethod]
public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
{ //...
[WebMethod]
公共静态字符串计算(IEnumerable AIAPP,Guid)
{ //...
在我的VS2013 web表单项目中,罪魁祸首是:
var settings=newfriendlyurlsettings{AutoRedirectMode=RedirectMode.Permanent};
使用FriendlyUrls的默认设置解决了这个问题--不要使用RedirectMode.Permanent
像这样的ajax调用,其中数据参数很复杂
$.ajax({
type: "POST",
contentType: "application/json",
url: applicationBaseUrl + "mypage.aspx/Calc",
data: params // data is json
}).success(function (data, status, xhr) {
//...
}).error(function (xhr, status, error) {
//...
});
WebMethod是这样的
[WebMethod]
public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
{ //...
[WebMethod]
公共静态字符串计算(IEnumerable AIAPP,Guid)
{ //...
在我的VS2013 web表单项目中,罪魁祸首是:
var settings=newfriendlyurlsettings{AutoRedirectMode=RedirectMode.Permanent};
使用FriendlyUrls的默认设置解决了这个问题--不要使用RedirectMode.Permanent
像这样的ajax调用,其中数据参数很复杂
$.ajax({
type: "POST",
contentType: "application/json",
url: applicationBaseUrl + "mypage.aspx/Calc",
data: params // data is json
}).success(function (data, status, xhr) {
//...
}).error(function (xhr, status, error) {
//...
});
WebMethod是这样的
[WebMethod]
public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
{ //...
[WebMethod]
公共静态字符串计算(IEnumerable AIAPP,Guid)
{ //...
在我的VS2013 web表单项目中,罪魁祸首是:
var settings=newfriendlyurlsettings{AutoRedirectMode=RedirectMode.Permanent};
使用FriendlyUrls的默认设置解决了这个问题--不要使用RedirectMode.Permanent
像这样的ajax调用,其中数据参数很复杂
$.ajax({
type: "POST",
contentType: "application/json",
url: applicationBaseUrl + "mypage.aspx/Calc",
data: params // data is json
}).success(function (data, status, xhr) {
//...
}).error(function (xhr, status, error) {
//...
});
WebMethod是这样的
[WebMethod]
public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
{ //...
[WebMethod]
公共静态字符串计算(IEnumerable AIAPP,Guid)
{ //...
使用
使用
使用
使用
你能在web方法中抛出一个调试断点,看看它是否击中了它吗?我自己就这么做了。我肯定需要JSON格式的参数。ScriptMethod不重要。我必须注释掉MyRow,因为我没有用它做任何事,我没有构建数据表并将其存储到会话,所以它总是空的。但是,它没有s返回“done”并输入WebMethod。我还假设您的标记为“OnClientClick”你正在阻止回发-上面的示例不是这样。谢谢…看起来我的VS中出现了问题。我将尝试重新创建此项目,然后再试一次。你在web方法中抛出一个调试断点,看看它是否命中了它?我自己就把它放在一起了。肯定需要JSON格式的参数。ScriptMethod不重要。我ad注释掉MyRow,因为我没有用它做任何事情,而且我没有将datatable构建并存储到会话,所以它总是空的。但是,它确实返回“done”并输入WebMethod。我还假设您的标记是“OnClient Click”你正在阻止回发-上面的示例不是这样。谢谢…看起来我的VS中出现了问题。我将尝试重新创建此项目,然后再试一次。你在web方法中抛出一个调试断点,看看它是否命中了它?我自己就把它放在一起了。肯定需要JSON格式的参数。ScriptMethod不重要。我ad注释掉MyRow,因为我没有用它做任何事情,而且我没有将datatable构建并存储到会话,所以它总是空的。但是,它确实返回“done”并输入WebMethod。我还假设您的标记是“OnClient Click”你正在阻止回发-上面的示例不是这样。谢谢…看起来我的VS中出现了问题。我将尝试重新创建此项目,然后再试一次。你在web方法中抛出一个调试断点,看看它是否命中了它?我自己就把它放在一起了。肯定需要JSON格式的参数。ScriptMethod不重要。我因为我没有对t做任何事情,所以我要注释掉MyRow