在url重写或url映射后,Jquery ajax不调用webmethod [英] Jquery ajax not calling webmethod after url rewriting or url mapping
问题描述
500 内部服务器错误
jQuery Ajax在url重写后没有调用webmethod
WebService.asmx
包含以下colde
[WebMethod]
公共字符串HelloWorld(){
返回Hello World;
}
-------------我的jquery代码-------------
< pre lang =JavaScript>< script type = text / javascript>
$( document )。ready( function ()
{
$ .ajax({
type: POST,
url:' WebService.asmx / HelloWorld',
data:{},
contentType: application / json; charset = utf-8,
成功:countItem
});
function countItem(data,status,xhr)
{
alert (data.d);
}
});
< / script>
它运行正常,但是当我使用url重写或url生根时给出错误
原始路径
http:// localhost:1388 / WebSite15 / WebService.asmx / HelloWorld
它会工作
但是在url重写或url rooting之后
http:// localhost:1388 / WebSite15 / Products / WebService.asmx / HelloWorld
所以它给出了错误
我尝试了什么:
void Application_Start(object sender,EventArgs e)
{
//在应用程序启动时运行的代码
//在应用程序启动时运行的代码
RegisterRoute(System.Web.Routing.RouteTable.Routes);
}
void RegisterRoute(System.Web.Routing.RouteCollection路线)
{
routes.Add(Pr产品,新的System.Web.Routing.Route({Products} / {category},新的CategoryRouteHandler()));
}
公共类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)
{
返回BuildManager.CreateInstanceFromVirtualPath(〜/ test1.aspx ,typeof(Page))as Page;
}
else
{
返回BuildManager.CreateInstanceFromVirtualPath( 〜/ Products.aspx,typeof(Page))为Page;
}
// string vartualpath = BuildManager.CreateInstanceFromVirtualPath( 〜/ Products.aspx,typeof(Page))。ToString();
//将BuildManager.CreateInstanceFromVirtualPath(〜/ category.aspx,typeof(Page))返回为Page;
}
}
( document ).ready( function ()
{
type: POST,
url: ' WebService.asmx / HelloWorld',
data:{},
contentType : application / json; charset = utf-8,
成功:countItem
});
function countItem(data,status, xhr)
{
alert(data.d);
}
});
< / script>
它工作正常但是当我使用url重写或url rooting时会出错
原始路径
http:// localhost:1388 / WebSite15 / WebService.asmx / HelloWorld
它会工作
但在url重写或url rooting之后
http:// localhost:1388 / WebSite15 / Products / WebService.asmx / HelloWorld
所以它给出了错误
我尝试过:
void Application_Start(object sender,EventArgs e)
{
//在应用程序启动时运行的代码
//代码在应用程序启动时运行
RegisterRoute(System.Web.Rout ing.RouteTable.Routes);
}
void RegisterRoute (System.Web.Routing.RouteCollection路线)
{
routes.Add(Products,new System.Web.Routing.Route({Products} / {category},new CategoryRouteHandler()));
}
公共类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)
{
返回BuildManager.CreateInstanceFromVirtualPath(〜/ test1.aspx ,typeof(Page))as Page;
}
else
{
返回BuildManager.CreateInstanceFromVirtualPath( 〜/ Products.aspx,typeof(Page))为Page;
}
// string vartualpath = BuildManager.CreateInstanceFromVirtualPath( 〜/ Products.aspx,typeof(Page))。ToString();
//将BuildManager.CreateInstanceFromVirtualPath(〜/ category.aspx,typeof(Page))返回为Page;
}
}
您好,
来自您的javascript代码我感觉到的是当你重写或映射时,url被更改并且不提供相同的url i在ajax网址。
你的网址:
' WebService.asmx / HelloWorld'
但是在映射为url更改后,ajax找不到相同的。
所以,我认为你在ajax中的网址应该是:
' / Products / WebService.asmx / HelloWorld'
注意:请在URL的开头加上一个/,以便在调用不同控制器的方法时能够正确路由。
请尝试发布查询(如果有的话)。
谢谢,
Prateek
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 -------------
<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;
}
}
(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;
}
}
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:'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:
'/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
这篇关于在url重写或url映射后,Jquery ajax不调用webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!