ASP.NET调用JS从AJAX非静态的WebMethod

编程入门 行业动态 更新时间:2024-10-28 18:29:13
本文介绍了ASP.NET调用JS从AJAX非静态的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述   

可能重复:结果  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

本文发布于:2023-11-24 22:31:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   NET   ASP   JS   WebMethod

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!