.ajax 帖子上出现 401 未经授权

发布于 2024-12-11 08:29:04 字数 2075 浏览 0 评论 0原文

当我的 ajax 执行 POST 事件时,我收到 401 Unauthorized,这仅在项目通过 sitecore 运行时出现。这一切就像一个魅力,我需要解决 sitecore 这是我当前的设置。

我按照链接 1 设置了我的代码,因此我有以下内容:

MasterLayout.aspx:

MasterLayout.aspx.cs:

using System.Web.Services;

[WebMethod]
public static string GetDate()
{
   return DateTime.Now.ToString();
}

MyCharts.ascx

暂时点击这里。

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 时尝试POST HelloWorld,我得到:

"Message":"Authenticationfailed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"

链接 1: http://encosia.com/using-jquery -to-directly-call-aspnet-ajax-page-methods/

相关问题:与 jquery 调用 webmethod 一致地获取“401 Unauthorized”错误

我的相关问题:https://stackoverflow.com/questions/7837532/get- my-html-and-javascript-to-perform-postback-ajax-call

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-12-18 08:29:04

如果此页面托管在 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}" />

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

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.

爱给你人给你 2024-12-18 08:29:04

一些挑剔可能会让您找到解决方案:

  1. 如果您不传递任何数据,则不需要包含“Data”属性
  2. 您正在使用“POST”,但没有发布任何数据 - 使用“ GET”而不是
  3. 你说你的 contentType 和 dataTypes 是 JSON,但 Web 方法返回一个字符串

post/get 可能会解决问题 - Get 的安全性比 Post 宽松得多。

添加错误回调函数也可能有所帮助,以查看是否有更多关于请求失败原因的信息。

A few nit-picks that may move you towards a solution:

  1. You don't need to include the "Data" property if you aren't passing any data
  2. You are using "POST" but you're not posting any data - use "GET" instead
  3. You say your contentType and dataTypes are JSON but the web method returns a string

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.

不必你懂 2024-12-18 08:29:04

我想要一个解决方案,使我能够允许我正在发布帖子的页面通过 sitecores 上下文运行。如果您不介意页面忽略上下文,请查看 Mark Ursino 的答案,它将像魅力一样发挥作用。

我的答案执行以下操作:

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.

javascript 在我的更新面板上强制执行回发事件,并解析我发送到网站后端的一串参数。 Onload 事件在回发时触发并触发我在 onload 事件中执行的任何操作,在本例中,将项目放入数组中以拆分它们,然后将它们放入数据库中。简单。感谢大家对此的帮助。 :D

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文