Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
C#
500 Internal Server Error


jQuery Ajax not calling webmethod after url rewriting

WebService.asmx
contain following colde

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
-------------my jquery code -------------

HTML
<script type="text/javascript">
    $(document).ready(function ()
    {

        $.ajax({
            type: "POST",
            url: 'WebService.asmx/HelloWorld',
            data: {},
            contentType: "application/json; charset=utf-8",
            success: countItem
        });
        function countItem(data, status, xhr)
        {

            alert(data.d);

        }


    });

</script>


it is working fine but when i use url rewrite or url rooting it give error

original path
http://localhost:1388/WebSite15/WebService.asmx/HelloWorld
it will work
but after url rewrite or url rooting
http://localhost:1388/WebSite15/Products/WebService.asmx/HelloWorld

so it gives error

What I have tried:

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// Code that runs on application startup
RegisterRoute(System.Web.Routing.RouteTable.Routes);

}


void RegisterRoute(System.Web.Routing.RouteCollection routes)
{




routes.Add("Products", new System.Web.Routing.Route("{Products}/{category}", new CategoryRouteHandler()));
}


public class CategoryRouteHandler : IRouteHandler
{
public CategoryRouteHandler() { }

public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// AdventureWorksEntities awe = new AdventureWorksEntities();
string cat = requestContext.RouteData.Values["category"] as string;
string pageval = requestContext.RouteData.Values["Products"] as string;
//int catid = awe.ProductCategories.Where(x => x.Name == cat).FirstOrDefault().ProductCategoryID;
HttpContext.Current.Items["catid"] = cat;
if (pageval.ToLower().ToString().Trim() == "test")
{

return BuildManager.CreateInstanceFromVirtualPath("~/test1.aspx", typeof(Page)) as Page;
}
else
{
return BuildManager.CreateInstanceFromVirtualPath("~/Products.aspx", typeof(Page)) as Page;
}

// string vartualpath=BuildManager.CreateInstanceFromVirtualPath("~/Products.aspx", typeof(Page)).ToString();
//return BuildManager.CreateInstanceFromVirtualPath("~/category.aspx", typeof(Page)) as Page;
}
}
Posted
Updated 17-May-18 2:32am
v2

Hi,

From your javascript code what I sense is While you are rewriting or mapping , the url is changed and the same url is not supplied in the ajax url.

your url:
JavaScript
'WebService.asmx/HelloWorld'


but after mapping as the url is changed, ajax is unable to find the same.

So, I think your url in ajax should be:

JavaScript
'/Products/WebService.asmx/HelloWorld'


Note: please put a "/" in the beginning of the url sothat it will be able route properly when a method of a different controller is called.

Please try and post queries if any.

Thanks,
Prateek
 
Share this answer
 
Comments
I think the same too.
I wrote a solution on Stack Overflow about this issue, check this link...
asp.net - jQuery Ajax not calling webmethod after url rewriting - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900