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
A few nit-picks that may move you towards a solution:
The post/get may fix the problem - Get is much more lax with security than Post.
It may also help to add an error callback function to see if there is any more information as to why the request is failing.
If this page is hosted in a Sitecore solution, which it seems like it is, the page request is probably going through the Sitecore context. You can edit the web.config
to cause this page to not go through the Sitecore context. There's a setting called IgnoreUrlPrefixes
:
<!-- IGNORE URLS
Set IgnoreUrlPrefixes to a '|' separated list of url prefixes that should not be
regarded and processed as friendly urls (ie. forms etc.)
-->
<setting name="IgnoreUrlPrefixes" value="{Pipe-delimited list}" />
You can update this list to include the path to your special page.
I wanted a solution that enabled me to allow the page I was doing the post on to be run through sitecores context. If you don't mind the page ignoring the context look at Mark Ursino answer it will work like a charm.
My answer does the following:
Javascript:
function MySuperCoolFunction() {
strUser1 = document.getElementById("Symbol1").innerHTML;
strUser2 = document.getElementById("Symbol2").innerHTML;
var join = strUser1 + "," + strUser2;
__doPostBack('upCurrencyCharts', join);
}
.ascx/.aspx:
<asp:UpdatePanel runat="server" ID="upCurrencyCharts" OnLoad="upCurrencyCharts_onload">
<ContentTemplate>
// My Content Here
</ContentTemplate>
</asp:UpdatePanel>
.cs:
protected void upCurrencyCharts_onload(object sender, EventArgs e)
{
string item1 = null;
string item2 = null;
if (IsPostBack)
{
string items = Request["__EVENTARGUMENT"];
string[] partsArray = items.Split(',');
for (int i = 0; i < partsArray.Length; i++)
{
if (i == 0)
{
item1 = partsArray[i];
}
if (i == 1)
{
item2 = partsArray[i];
}
}
}
// do something with item1 and 2 here.. in mycase feed to database.
The javascript forces a postback event on my update panel, and parses a string of parameters I am sending through to the backend of my website. Onload event triggers on postback and fires anything I do in my onload event in this case puts the items into an array to split them and then puts them into a database. Simples. Thanks everyone for there help in this. :D