如何使用Ajax从Aspx.Cs页面调用方法。 [英] How Do I Call The Method From Aspx.Cs Page Using Ajax.
问题描述
我的WebForm1.aspx代码如下:
<%@ < span class =code-attribute> Page 标题 = 语言 = C# < span class =code-attribute> MasterPageFile = 〜/ Site.Master AutoEventWireup = true
CodeBehind = WebForm1.aspx.cs Inherits = Kaizen.UI.WEB.Kaizen2G.WebForm1 %>
< asp:内容 ID < span class =code-keyword> = Content1 ContentPlaceHolderID = HeadContent runat = 服务器 >
< / asp:Content >
< span class =code-keyword>< asp:Content ID = Content2 ContentPlaceHolderID = MainContent runat = < span class =code-keyword> server >
< script >
setInter val(function(){CheckSession(); }, 10000 );
函数CheckSession(){
debugger;
$ .ajax({
url:' WebForm1.aspx / CheckSession' ,
类型:' POST',
contentType:' application / json; charset = utf-8',
dataType: json,
成功:函数(结果){
if (result.d!= ' '){
alert (result.d);
manageOverlay( false );
isAdded = false ;
}
},
错误:函数(呃ror){
manageOverlay( false );
alert(error.responseText);
isAdded = false ;
},
async : false
} )
};
< / script >
我的.cs页面中的代码如下
命名空间 Kaizen.UI.WEB.Kaizen2G
{
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 Kaizen.Framework;
/// < 摘要 >
/// 页面looad
/// < / summary >
public partial class WebForm1:System.Web.UI.Page
{
/// < 摘要 >
/// 检查用户会话
/// < / summary >
[WebMethod]
public void CheckSession()
{
if (Session [KaizenConstants.CURRENTLOGGEDINUSER] == null )
{
Response.Redirect( Login.aspx) ;
}
}
/// < 摘要 >
/// 页面事件
// / < / summary >
/// < param name =sender > 发件人< / param >
/// < param name =e > Eventsargs < / param >
protected void Page_Load( object sender,EventArgs e)
{
}
}
}
I have the code in my WebForm1.aspx as follows: 这篇关于如何使用Ajax从Aspx.Cs页面调用方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
页面加载aspx页面上的
我写道:
<前lang =Javascript> $(功能(){
尝试 {
调试器跨度>;
$ .ajaxSetup({
async: false
});
setInterval( function (){CheckSession();}, 10000 ) ;
}
catch (例外){
manageOverlay( false ) ;
alert(例外);
}
});
现在当我从ajax调用方法时,我收到的错误如下。
500(内部服务器错误)
为什么会这样。
请指导我。
i想从调用方法WebForm1.aspx.cs
谢谢
Harshal .ajax({
url: WebForm1.aspx / CheckSession',
类型:' POST',
contentType:' application / json; charset = utf-8',
dataType: json,
成功:函数(结果){
if (result.d!= ' '){
警报(result.d);
manageOverlay( false );
isAdded = false ;
}
},
错误:函数(错误){
manageOverlay( false );
alert(error.responseText);
isAdded = false ;
},
async : false
} )
};
< / script >
我的.cs页面中的代码如下
命名空间 Kaizen.UI.WEB.Kaizen2G
{
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Services;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 Kaizen.Framework;
/// < 摘要 >
/// 页面looad
/// < / summary >
public partial class WebForm1:System.Web.UI.Page
{
/// < 摘要 >
/// 检查用户会话
/// < / summary >
[WebMethod]
public void CheckSession()
{
if (Session [KaizenConstants.CURRENTLOGGEDINUSER] == null )
{
Response.Redirect( Login.aspx) ;
}
}
/// < 摘要 >
/// 页面事件
// / < / summary >
/// < param name =sender > 发件人< / param >
/// < param name =e > Eventsargs < / param >
protected void Page_Load( object sender,EventArgs e)
{
}
}
}
页面加载aspx页面上的
我写道:
( function (){
尝试 {
调试器;
.ajaxSetup({
async: false
});
setInterval( function (){CheckSession();}, 10000 ) ;
}
catch (例外){
manageOverlay( false ) ;
alert(例外);
}
});
现在当我从ajax调用方法时,我收到的错误如下。
500(内部服务器错误)
为什么会这样。
请指导我。
i想从调用方法WebForm1.aspx.cs
谢谢
Harshal
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="Kaizen.UI.WEB.Kaizen2G.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script>
setInterval(function () { CheckSession(); }, 10000);
function CheckSession() {
debugger;
$.ajax({
url: 'WebForm1.aspx/CheckSession',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (result) {
if (result.d != '') {
alert(result.d);
manageOverlay(false);
isAdded = false;
}
},
error: function (error) {
manageOverlay(false);
alert(error.responseText);
isAdded = false;
},
async: false
})
};
</script>
I have code in my .cs page as follows
namespace Kaizen.UI.WEB.Kaizen2G
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kaizen.Framework;
/// <summary>
/// The page looad
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{
/// <summary>
/// Check User Session
/// </summary>
[WebMethod]
public void CheckSession()
{
if (Session[KaizenConstants.CURRENTLOGGEDINUSER] == null)
{
Response.Redirect("Login.aspx");
}
}
/// <summary>
/// The page event
/// </summary>
/// <param name="sender">The Sender</param>
/// <param name="e">The Eventsargs</param>
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
on page load of aspx page i wrote this :
$(function () {
try {
debugger;
$.ajaxSetup({
async: false
});
setInterval(function () { CheckSession(); }, 10000);
}
catch (exception) {
manageOverlay(false);
alert(exception);
}
});
Now when i am calling method from ajax i am getting the error as follows.
500(internal Server Error)
why is it so .
please guide me .
i want to call the method from WebForm1.aspx.cs
Thanks
Harshal.ajax({
url: 'WebForm1.aspx/CheckSession',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (result) {
if (result.d != '') {
alert(result.d);
manageOverlay(false);
isAdded = false;
}
},
error: function (error) {
manageOverlay(false);
alert(error.responseText);
isAdded = false;
},
async: false
})
};
</script>
I have code in my .cs page as follows
namespace Kaizen.UI.WEB.Kaizen2G
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kaizen.Framework;
/// <summary>
/// The page looad
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{
/// <summary>
/// Check User Session
/// </summary>
[WebMethod]
public void CheckSession()
{
if (Session[KaizenConstants.CURRENTLOGGEDINUSER] == null)
{
Response.Redirect("Login.aspx");
}
}
/// <summary>
/// The page event
/// </summary>
/// <param name="sender">The Sender</param>
/// <param name="e">The Eventsargs</param>
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
on page load of aspx page i wrote this :
(function () {
try {
debugger;
.ajaxSetup({
async: false
});
setInterval(function () { CheckSession(); }, 10000);
}
catch (exception) {
manageOverlay(false);
alert(exception);
}
});
Now when i am calling method from ajax i am getting the error as follows.
500(internal Server Error)
why is it so .
please guide me .
i want to call the method from WebForm1.aspx.cs
Thanks
Harshal