I'm trying to map an incoming URL from an ajax request to a web service method in the Global.asax.
The web service is in this path /ajax/BookWebService.asmx
, it has 2 methods GetListOfBook
and GetListOfAuthor
.
I want to use url: /ajax/book/list
instead of url: /ajax/BookWebService.asmx/GetListOfBook
in the Script tag.
Here's my script and mark-up:
<script type="text/javascript">
$(function () {
$("#btnCall").click(function () {
$.ajax({type: "POST",
url: "/ajax/book/list",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var list = response.d;
$('#callResult').html(list);
},
error: function (msg) {
alert("Oops, something went wrong");
}
});
});
});
</script>
<div>
<div id="callResult"></div>
<input id="btnCall" type="button" value="Call Ajax" />
</div>
Here's the Global.asax :
void Application_BeginRequest(object sender, EventArgs e)
{
var currentRequest = HttpContext.Current.Request.Url.AbsolutePath;
if (currentRequest.Contains("ajax/book/list")) {
HttpContext.Current.RewritePath("/ajax/BookWebService.asmx/GetListOfBook");
}
}
When I check in FireBug console this is what I get :
"NetworkError: 500 Internal Server Error - localhost:13750/ajax/book/list"
If I change the url: /ajax/book/list
to url: /ajax/BookWebService.asmx/GetListOfBook
it works as expected.
I'm using VS 2010 and .NET 4.
How do I do ajax call to /ajax/book/list
instead of /ajax/BookWebService.asmx/GetListOfBook
?