我需要从Ajax使用的WebMethod向页面发送一个脚本,点击HTML链接时会触发该脚本。我无法将脚本管理器与“我”或“页面”控件一起使用,也无法引用任何控件。
我只需要返回那个会话,任何想法?
点击发送Ajax的按钮是HTML链接,所有我需要检查会话是否过期(我可以在加载时检查它)所以如果它已过期想要提醒用户,因为我已经在检查后没有完成该过程在代码背后
<WebMethod()> _
Public Shared Function Send(ByVal data As String) As String
If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then
Try
''''' my code
''''''''''''''''''''''
If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then
Return "Success"
Else
Return "noSession"
End If
Catch ex As Exception
Return "failure"
End Try
Else
ScriptManager.RegisterStartupScript(Me, GetType(String), "Checkeng", [String].Format("LevelsMsg();"), True)
End If
End Function
它更加复杂,但我认为这是主要部分:
$(document).on("click", "#Add", function() {
var _fulldata = $('#basket').html();
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Order.aspx/SendOrder',
data: "{'fulldata':'" + _fulldata + "'}",
async: false,
success: function(response) {
},
error: function() {
alert("There is a problem in saving data");
}
});
});
答案 0 :(得分:1)
您的WebMethod
是Shared
函数,相当于C#中的Static
函数。这意味着您将无法访问除此Shared
函数内部声明的变量之外的任何变量。但是,WebMethods
的性质允许返回&#34;它的&#34;来电通过&#34;成功&#34;或&#34;错误&#34;可以&#34;截获&#34;。因此,无需使用ScriptManager.RegisterStartupScript
,因为您的POST无论如何都将返回到AJAX领域,这意味着您可以在那里调用任何JavaScript函数。
您可以通过以下方式更改代码:
VB.NET代码隐藏:
<WebMethod()> _
Public Shared Function Send(ByVal data As String) As String
If Not System.Web.HttpContext.Current.Session("MemberID") Is Nothing Then
Try
' YOUR CODE
Return "Success"
Catch ex As Exception
Return "Failure"
End Try
Else
Return "NoSession";
End If
End Function
<强> JavaScript的:强>
$(document).on("click", "#Add", function() {
var _fulldata = $('#basket').html();
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Order.aspx/SendOrder',
data: "{'fulldata':'" + _fulldata + "'}",
async: false,
success: function(response) {
/* since we put "NoSession", then we should check for it here */
if(response.d == "NoSession") {
/* This is where you "replace" the use of RegisterStartupScript
by safely calling any JS function here */
LevelsMsg();
}
},
error: function() {
alert("There is a problem in saving data");
}
});
});