ASP.NET调用JS从AJAX非静态的WebMethod [英] ASP.NET calling non-static webmethod from JS AJAX

查看:531
本文介绍了ASP.NET调用JS从AJAX非静态的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-sideaspx-cs-from-client-side-use-javascript\">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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆