weixin_33717117 2011-10-21 13:44 采纳率: 0%
浏览 69

401未经授权在.ajax帖子上

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

  • 写回答

3条回答 默认 最新

  • weixin_33701617 2011-10-21 15:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型