我得到一個401未經授權,當我的ajax正在做一個POST事件時,這隻有當項目正在通過sitecore運行時。這一切都像一個魅力,我需要爲sitecore工作這是我目前的設置。401未經授權.ajax文章
我跟着鏈接1設置我的代碼,所以我有以下幾點:
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.as CX
<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);
}
});
});
});
當調試JavaScript嘗試開機自檢的HelloWorld,我得到:
"Message":"Authenticationfailed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"
鏈接1:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
回覆遲來問:Getting "401 Unauthorized" error consistently with jquery call to webmethod
我的相關問題:https://stackoverflow.com/questions/7837532/get-my-html-and-javascript-to-perform-postback-ajax-call
「HelloWorld」WebMethod確實存在嗎?我知道這會導致404不是401,但是你的帖子並不是很清楚你想調用哪個'WebMethod' – simonlchilds
已編輯的代碼,它是GetDate我沒有收到HelloWorld。 – Anicho