获得" 401未授权"错误一贯使用jQuery调用的WebMethod [英] Getting "401 Unauthorized" error consistently with jquery call to webmethod

查看:305
本文介绍了获得" 401未授权"错误一贯使用jQuery调用的WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让我的jQuery调用一个WebMethod工作。我被一个401未授权的响应服务器的反弹。我必须在web.config中不正确的设置或者别的地方,这将是preventing的成功调用。

I have been struggling to get my jquery call to a webmethod to work. I am being bounced by the server with a "401 Unauthorized" response. I must have an incorrect setting in the web.config or somewhere else that would be preventing a successful call.

您的洞察力AP preciated!

Your insight is appreciated!

button.OnClickAction = "PageMethod('TestWithParams', ['a', 'value', 'b', 2], 'AjaxSucceeded', 'AjaxFailed'); return false;";

JavaScript函数,使得jQuery的通话

function PageMethod(fn, paramArray, successFn, errorFn) {
var pagePath = window.location.pathname;
var urlPath = pagePath + "/" + fn;

//Create list of parameters in the form:  
//{"paramName1":"paramValue1","paramName2":"paramValue2"}  
var paramList = '';
if (paramArray.length > 0) {
    for (var i = 0; i < paramArray.length; i += 2) {
        if (paramList.length > 0) paramList += ',';
        paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
    }
}
paramList = '{' + paramList + '}';

//Call the page method
$.ajax({
    type: "POST",
    url: pagePath + "/" + fn,
    contentType: "application/json; charset=utf-8",
    data: paramList,
    timeout: 10000,
    dataType: "json",
    success: function(result) { alert('Overjoyed'); },
    error: function(result) { alert('No joy'); }
});
}

页面Web方法

    public partial class WebLayout : System.Web.UI.Page
{

    [WebMethod()]
    public static int TestNoParams()
    {
        return 1;
    }

    [WebMethod()]
    public static string TestWithParams(string a, int b)
    {
        return a + b.ToString();
    }
...

如被看见在Firebug控制台响应

json: {"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}

"NetworkError: 401 Unauthorized - http://localhost/Care-Provider-Home/Profile/Personal-Profile.aspx/TestWithParams" TestWithParams

我已经看过并阅读关于这个问题(Encosia等)通常的网站,但无济于事。无论是我缺少一个关键部分,还是有一些细微之处,我环境的安全性参数preventing的电话。

I have looked at and read the usual sites on the subject (Encosia, et al), but to avail. Either I am missing a critical piece, or there are some subtleties in the security parameters of my environment that preventing a call.

下面是可能会影响您诊断其他一些可能有用的花絮:

Here are some other potentially useful tidbits that may impact your diagnosis:


  • 的Webmethods在codebehind

  • 使用Sitecore的CMS(好象没有intefere,永远不知道)

  • IIS7

  • .NET 3.5

  • 的jQuery 1.3.2

我期待着您的见解和方向 - 谢谢你

I look forward to your insights and direction--thank you!

推荐答案

是的,它没有得到工作!由于Sitecore的CMS不执行URL重写,生成友好的URL(它在组装层的页面,动态,类似于母版页概念),它发生,我认为它可能会造成一些问题最初导致401错误。我验证了这一点通过创建一个ASPX一个单独的项目 - 与一些工作,我可以调用Web方法和使用jQuery获取值。然后,我在我的网站根目录中创建几乎相同的ASPX,但他告诉Sitecore的忽略它当一个请求到它(在web.config IgnoreUrl prefixes)制成,经过一番工作,我能够也让它顺利进行!感谢您的帮助。

Yes, it did get working! Since Sitecore CMS does perform URL rewriting to generate friendly URLs (it assembles the pages in layers, dynamically, similar to Master Page concept), it occurred to me that it may be causing some problem the initially caused the 401 error. I verified this by creating a separate project with a single ASPX--and with some work I was able call the web methods and get values using the jquery. I then created nearly identical ASPX in my web root, but told Sitecore to ignore it when a request is made to it (IgnoreUrlPrefixes in the web.config), after some work I was able also get it to work successfully! Thanks for your help.

这篇关于获得&QUOT; 401未授权&QUOT;错误一贯使用jQuery调用的WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆