.ajax 帖子上出现 401 未经授权
当我的 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/
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果此页面托管在 Sitecore 解决方案中(看起来确实如此),则页面请求可能会通过 Sitecore 上下文。您可以编辑
web.config
以使此页面不经过 Sitecore 上下文。有一个名为IgnoreUrlPrefixes
的设置:您可以更新此列表以包含特殊页面的路径。
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 calledIgnoreUrlPrefixes
:You can update this list to include the path to your special page.
一些挑剔可能会让您找到解决方案:
post/get 可能会解决问题 - Get 的安全性比 Post 宽松得多。
添加错误回调函数也可能有所帮助,以查看是否有更多关于请求失败原因的信息。
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.
我想要一个解决方案,使我能够允许我正在发布帖子的页面通过 sitecores 上下文运行。如果您不介意页面忽略上下文,请查看 Mark Ursino 的答案,它将像魅力一样发挥作用。
我的答案执行以下操作:
Javascript:
.ascx/.aspx:
.cs:
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:
.ascx/.aspx:
.cs:
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