【问题标题】:jquery ajax call not working with asp.net url rewritejquery ajax 调用不适用于 asp.net url 重写
【发布时间】:2014-11-11 07:05:26
【问题描述】:

我正在尝试使用网络服务方法。我还在我的应用程序中使用 url 重写。

我的 ajax jquery 调用如下

   $.ajax({
            type: "POST",
            url: "dataone.asmx/lastName",
            data: JSON.stringify({ "firstname": $("#el").val(),"lastname":"Ali" }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            error: function (errorThrown) {
                console.log(errorThrown + "   " + errorThrown);
                alert(errorThrown.responseText + "what's wrong?" + " " + errorThrown);
            },
            success: function (msg) {
                console.log(msg.d);
                alert(msg.d);
                return false;
                // Do something interesting here.
            }
        });

我的网络方法正在跟踪

 [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//, ResponseFormat = ResponseFormat.Json
public string lastName(string firstname,string lastname)
{
    return  firstname == "fawad" ? "ali" : "first name incorrect"+lastname;
}

我的友好网址是

 <a href="edmonton/clickme">click</a>

在我的 global.asax 中,我正在按照以下方式进行 url 重写

HttpContext.Current.RewritePath(originalPath.Replace("edmonton/clickme", @"Default.aspx"));

但是我的网络方法没有被调用。当我删除 url 重写我的 ajax 调用开始工作

如果你们有任何想法可能是什么问题,请在下面的答案中发布。

谢谢

【问题讨论】:

    标签: jquery asp.net .net ajax url-rewriting


    【解决方案1】:

    在重写规则中添加以下条件。

    <add input="{HTTP_X_Requested_With}" pattern="^XMLHttpRequest$" negate="true" />
    

    这将忽略 XHR 请求。

    示例:在以下规则中,我跳过了图像文件、css、js 等和 XHR 请求的重写。希望这会有所帮助。

    <rule name="RuleXYZ" stopProcessing="true">  
          <match url="(.*)\..+"/>
          <conditions logicalGrouping="MatchAll">
              <add input="{URL}" negate="true" pattern="\.axd|\.jpg|\.png|\.css|\.js|\.ico$" />
              <add input="{HTTP_X_Requested_With}" pattern="^XMLHttpRequest$" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Temporary" url="/{R:1}" appendQueryString="false" />  
        </rule> 
    

    【讨论】:

      【解决方案2】:

      请试试下面这个。

          protected void Application_BeginRequest(object sender, EventArgs e)
          {
              if (HttpContext.Current.Request.RawUrl == "/edmonton/clickme")
                  HttpContext.Current.RewritePath("/Default.aspx");
          }
      

      【讨论】:

      • 请告诉我你在哪里触发 ajax 调用?在调用 Default.aspx?
      • 没有。在 default.aspx 中单击按钮时调用它。问题是由于url重写,在ajax调用中找不到我的webservice路径。
      • 嗯,&lt;a href="edmonton/clickme"&gt;click&lt;/a&gt; 来自另一个页面,您正在尝试将其重新路由到 default.aspx?并且default.aspx 包含可以触发ajax 调用的按钮?告诉我我是否正确......或者请进一步解释。谢谢
      • 我在本地尝试了上述场景并且效果很好。我想让你做的是检查 Request.RawUrl(调试它)。并比较您正在调用的 Url 和您分配的路径 (/edmonton/clickme)。有时空格或斜杠有问题。
      【解决方案3】:

      尝试将其添加到您的全局 asax 中,只需确保“clickme”位于默认页面代码后面

      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          if (HttpContext.Current.Request.RawUrl == "/edmonton/clickme")
              HttpContext.Current.RewritePath("/Default.aspx/clickme");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 2011-04-04
        • 2011-09-04
        • 1970-01-01
        • 2016-01-02
        相关资源
        最近更新 更多