简体   繁体   中英

jquery ajax call not working with asp.net url rewrite

I am trying to use web service method. I am also using url rewriting in my application.

My ajax jquery call is following

   $.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.
            }
        });

And my webmethod is following

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

And my friendly url is as

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

And in my global.asax I am doing url rewriting as following

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

But my web method is not getting called. When I remove url rewriting my ajax call start working

If any of you have any idea what might be a problem then please post it in answer below.

Thanks

Please try this one below.

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

Try to add this in your global asax just make sure that "clickme" is in Default page code behind

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

Add the below condition in the rewrite rule.

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

This will ignore XHR requests.

Example: In the below rule I am skipping the rewrite for image files, css, js, etc and XHR requests. Hope this helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM