【问题标题】:page web method not working in ajax call where web service method is calling perfectly页面 web 方法在 web 服务方法完美调用的 ajax 调用中不起作用
【发布时间】:2012-07-27 13:08:57
【问题描述】:

我想为价格范围实现 jquery 滑块,我尝试使用 jquery ajax 调用页面 web 方法,但它在网页方法的情况下不起作用,但如果我只是将 ajax 调用的 URL 属性更改为webservice 然后完美调用。我从几个小时开始尝试这个,并没有找到这背后的任何逻辑。这是我的代码

<script type="text/javascript">
        var startPosition;
        $(document).ready(function () {          
            var hdnMinPrice = 142;
            var hdnMaxPrice = 969;
            $("#slider").slider(
              {
                  min: hdnMinPrice,
                  max: hdnMaxPrice,
                  range: true,
                  values: [hdnMinPrice, hdnMaxPrice],
                  step: 50,
                  slide: function (event, ui) {
                      $('#lbl').text(ui.values[0] + ' - ' + ui.values[1]);

                  },
                  start: function (event, ui) {
                      startPosition = ui.value;
                      //alert('Slider started at: ' + ui.value);
                  },
                  stop: function (event, ui) {                     
                      $.ajax({
                          type: "POST",
                          //url: SearchResult.aspx/FilterByPrice",                                                    
                          url: "WebService.asmx/InsetSubscriber",
                          data: "{email: '250@yahoo.com'}",
                          contentType: "application/json; charset=utf-8",
                          dataType: "json",
                          success: function (msg) {
                              alert('Thanks');
                              // Do something interesting here.
                          }
                      });                     
                      return false;
                  }
    });
        });
    </script>

注释掉的URL选项是页面web方法,下面是web页面方法的定义

 [WebMethod]

    public void FilterByPrice(string email)
    {
        Response.Write("min" + email);
        //Response.Write("max" + max);
    }

其中的网络服务方法如下:

[WebMethod]
    public void InsetSubscriber(string email)
    {
        DALSubscriber objSubscriber = new DALSubscriber();
        objSubscriber.InsertSubscriber(email);

    }

在此代码段之后,我再次在这里重复我的问题。 在 jquery ajax 调用中运行使用 Web 服务方法的相同方法时,页面 Web 方法不起作用

【问题讨论】:

  • 所以在 Ajax 中使用 url 调用它时不起作用:SearchResult.aspx/FilterByPrice 对吗?

标签: c# asp.net web-services jquery webmethod


【解决方案1】:

这是因为 PageMethods 在您的页面上必须是静态的

试试这个:

[WebMethod]
public static void FilterByPrice(string email)
{
    // Use HttpContext.Current.Response instead
    // Response.Write("min" + email);
    //Response.Write("max" + max);
}

【讨论】:

    【解决方案2】:

    我建议你在 httpmodule 部分检查你的 web.config。它必须具有 ScriptManager 的 ScriptModule,因为网页方法可以使用它

    <system.web>
      <httpModules>
        <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </httpModules>
    </system.web>
    

    【讨论】:

      【解决方案3】:

      添加 EnablePageMethods="True" 和 EnableScriptGlobalization="True"

      希望它会起作用

      【讨论】:

      • 这应该是评论而不是答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多