<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '/Default.aspx/GetData',
type: 'POST',
beforeSend: function( xhr ) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
success: function (result) {
var resultData = (result.d? result.d : result);
alert(resultData);
},
error : function(){
alert('error');
}
});
});
</script>
[System.Web.Services.WebMethod]
public static string GetData()
{
return "Hello";
}
Add FriednlyUrls to the project.
Remove the line in RegisterRoutes method that sets settings.AutoRedirectMode property in App_Start/RouteConfig.cs
(note that setting it to RedirectMode.Permanent or RedirectMode.Off did NOT work for me)
Add authorization in web.config as follows under system.web section
<authorization>
<allow users="*" />
</authorization>
Modify the url in ajax call set up to use Microsoft.AspNet.FriendlyUrls.Resolve function in order to get the correct url as below:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '<%=Microsoft.AspNet.FriendlyUrls.FriendlyUrl.Resolve("/Default.aspx/GetData")%>',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result) {
var resultData = (result.d? result.d : result);
alert(resultData);
},
error : function(){
alert('error');
}
});
});
</script>
Links : https://stackoverflow.com/questions/32663858/jquery-ajax-calls-not-working-with-asp-net-web-forms-when-friendlyurls-are-added
Comments
Post a Comment