I get a 401 Unauthorized when my ajax is doing a POST event this is only when the project is being run through sitecore. This all works like a charm I need a work around for sitecore this is my current setup.
I followed Link 1 to setup my code, so I have the following:
MasterLayout.aspx:
<script src="jsfile.js" type="text/javascript"></script>
<script src="../../../content/javascript/jquery-1.6.2.min.js"></script>
MasterLayout.aspx.cs:
using System.Web.Services;
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
MyCharts.ascx
<div class="Result" id="Result">Click here for the time.</div>
jsfile.js:
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$('.Result').click(function() {
$.ajax({
type: "POST",
url: "MyCharts.aspx/GetDate", // MyCharts.aspx is a logical page created in sitecore physically it loads MasterLayout with MyCharts within that.
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$(".Result").text(msg.d);
}
});
});
});
When debugging javascript tries to POST HelloWorld and I get:
"Message":"Authenticationfailed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"
Link 1: http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Related Question: Getting "401 Unauthorized" error consistently with jquery call to webmethod
My Related Question: https://stackoverflow.com/questions/7837532/get-my-html-and-javascript-to-perform-postback-ajax-call