【问题标题】:jQuery Ajax not calling webmethod after url routingjQuery Ajax 在 url 路由后不调用 webmethod
【发布时间】:2011-08-16 15:27:12
【问题描述】:

我的 jquery ajax 函数没有调用 webmethod。 jquery 函数返回 web 服务页面的 html。 函数不明白“ebulten_add”是一个webmethod!

"url:ajaxPage.aspx/e_bulten"

写webmethod名字或者不写都是一样的..都返回ajaxPage.aspx html。

$.ajax({
                type: "POST",
                url: 'ajaxPage.aspx/ebulten_Add',
                data: "{ebEmail:'" + Ebemail + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {

                    $("#span_result").hide();
                    $("#span_spinner").hide();
                    $("#span_result").html(result.d).fadeIn();
                },
                error: function (msg) {                     
                    $("#span_result").hide();
                    $("#span_spinner").hide();
                    $("#span_result").html("Lütfen tekrar deneyin.").fadeIn();
                }
            });`

ajaxPage.aspx 中的 web 方法

    [System.Web.Services.WebMethod]
public static string ebulten_Add(string ebEmail)
{
    if (ebEmail == "Email")
    {
        return "*Bilgilerinizi Girmediniz";
    }
    else
    {
        List<ListItem> ebList = new List<ListItem>();           
        ebList.Add(new ListItem("@Eb_email", ebEmail));
        BL.Atom.GetByVoid("spEbulten_Add", ebList);
        return "*E-Bülten kaydınız başarıyla tamamlanmıştır";            
    }
}

【问题讨论】:

    标签: c# web-services jquery webmethod


    【解决方案1】:

    如我所见,您返回的是字符串而不是 json

    所以只需更新您的 dataType: 'text' 就可以了

    【讨论】:

      【解决方案2】:

      同意@SenadM。更改 dataType:text 或从您的网络方法返回 JSON:

      [System.Web.Services.WebMethod]
      public static string ebulten_Add(string ebEmail)
      {
          if (ebEmail == "Email")
          {
              return "{ \"response\": \"*Bilgilerinizi Girmediniz\"}";
          }
          else
          {
              List<ListItem> ebList = new List<ListItem>();           
              ebList.Add(new ListItem("@Eb_email", ebEmail));
              BL.Atom.GetByVoid("spEbulten_Add", ebList);
              return "{ \"response\": \"*E-Bülten kaydiniz basariyla tamamlanmistir\"}";            
          }
      }
      

      另外,请确保在您的 web.config 中启用 POST:

      <configuration>
          <system.web>
          <webServices>
              <protocols>
                  <!-- <add name="HttpGet"/> --> <!-- uncomment to enable get -->
                  <add name="HttpPost"/>
              </protocols>
          </webServices>
          </system.web>
      </configuration>
      

      【讨论】:

      • 代码进入成功函数,但仍然返回ajaxPage html。网络方法不起作用
      • 为什么是ajaxPage.aspx中的方法?它应该是一个网络服务:ajaxPage.asmx
      • 是的,我也有 ajaxPage.asmx。但它与 asmx 相同。我尝试了两页
      • 注意到另一件事:您的数据不是有效的 json,应该是:data: '{ "ebEmail" : "' + Ebemail + '" }',
      • 查看我的更新。也许 POST 在您的web.config 中被禁用(默认情况下)。
      【解决方案3】:

      只需将var settings = new FriendlyUrlSettings {AutoRedirectMode = RedirectMode.Permanent}; 更改为var settings = new FriendlyUrlSettings {AutoRedirectMode = RedirectMode.Off}; 这应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        • 2011-12-07
        相关资源
        最近更新 更多