簡體   English   中英

jQuery Ajax調用不能與asp.net url重寫一起使用

[英]jquery ajax call not working with asp.net url rewrite

我正在嘗試使用Web服務方法。 我也在應用程序中使用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中,我按以下方式進行網址重寫

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

但是我的網絡方法沒有被調用。 當我刪除網址重寫時,我的ajax調用開始工作

如果您有任何想法可能是什么問題,請在下面的答案中發布。

謝謝

請在下面嘗試這個。

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

嘗試將其添加到全局asax中,只需確保“ clickme”位於后面的默認頁面代碼中

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

在重寫規則中添加以下條件。

<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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM