当我的ajax执行POST事件时,仅在通过Sitecore运行项目时,我得到401 Unauthorized。所有这一切都像一个魅力,我需要为sitecore解决,这是我当前的设置。

我按照链接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.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);
            }
        });
    });
});


当调试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

最佳答案

如果此页面托管在Sitecore解决方案中(看起来像是这样),则该页面请求可能会通过Sitecore上下文。您可以编辑web.config,以使该页面不通过Sitecore上下文。有一个名为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}" />


您可以更新此列表以包括指向特殊页面的路径。

07-24 09:38
查看更多