可能重复:结果 Call从客户端服务器端(aspx.cs)非静态方法使用JavaScript(CSS)
我有这个以下code,它是工作的罚款
函数getFooObj(){ $阿贾克斯({ 键入:POST, 网址:Dummy.aspx / GetFooObj 的contentType:应用/ JSON的;字符集= UTF-8, 数据类型:JSON 成功:函数(结果){ 警报(好); } });}[的WebMethod]公共静态FooObj GetFooObj(){ //一些code返回FooObj}我的问题是,如果我想我的WebMethod不能静态的,我怎么能叫从JS?
[的WebMethod]公共FooObj GetFooObj(){ //一些code返回FooObj}解决方案
不可能的 - PageMethods必须是静态的。
原因很简单 - 一个实例(非静态)方法意味着它可以访问包括控制页面的状态。但是,ASP.NET页/控制模式需要确保的控件一致的状态状态信息(视图状态,事件验证等)。但在页面方法的情况下,这是因为完整的形式是不是回发是不可能的(这基本上是落后PageMethods / ScriptServices的想法 - 你发送/接收客户机/服务器之间只有最低限度的信息)。
有关使用实例方法(假设你需要控制访问),你应该使用AJAX做的UpdatePanel的方法。
Possible Duplicate: Call non-static method in server side(aspx.cs) from client side use javascript (aspx)
I have this following code that is working fine
function getFooObj() { $.ajax({ type: "POST", url: "Dummy.aspx/GetFooObj", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { alert('good'); } }); } [WebMethod] public static FooObj GetFooObj () { // some code that returns FooObj }my question is if i want my WebMethod NOT to be static, how can i call it from JS?
[WebMethod] public FooObj GetFooObj () { // some code that returns FooObj }解决方案
Not possible - PageMethods has to be static.
Reason is quite simple - a instance (non-static) method means it can access page state including controls. However, ASP.NET page/control model needs state information (view-state, event validation etc) for ensuring consistent state of controls. But in case of Page Methods, this is not possible because complete form is not posted back (which is essentially the idea behind PageMethods/ScriptServices - you send/receive only bare minimum information between client/server).
For using instance methods (assuming that you need control access), you should use UpdatePanel way of doing AJAX.
更多推荐
ASP.NET调用JS从AJAX非静态的WebMethod
发布评论