jQuery Ajax调用失败,未授权401 [英] jquery ajax call fails with 401 unauthorized

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

问题描述

我有一个jquery ajax POST到代码隐藏的webmethod.在该网络方法中,我对返回json的第三方网络api服务执行HttpWebRequest.即使httpwebrequest正常运行,浏览器中也会出现一个弹出窗口,要求我输入凭据(需要身份验证).在我的计算机上,此方法运行良好,但是在部署时,除非httpwebrequest调用未返回任何数据,否则它不能正常工作.

I have a jquery ajax POST to a code-behind webmethod. In that webmethod i do a HttpWebRequest to a third party web api service that returns json. Even though the httpwebrequest works fine, a popup appears in the browser asking me to enter credentials (authentication required). On my machine this works well, however when deployed it doesn't except if there is no data returned from the httpwebrequest call.

jquery调用:

function serverCall(httpMethod, pageName, methodName, inputData, successCallback, errorCallback, disableGlobalAjaxEvents) {

    // Construct the url
    var url = pageName + "/" + methodName;    

    var triggerGlobalEvents = true;
    if (disableGlobalAjaxEvents && disableGlobalAjaxEvents == true) {
        triggerGlobalEvents = false;
    }

    $.ajax({
        type: httpMethod,
        url: url,
        data: JSON.stringify(inputData),
        contentType: "application/json; charset=utf-8",
        global: triggerGlobalEvents,
        dataType: "json",
        success: function(msg) {
            if (successCallback) {

                var parsedObject = JSON.parse(msg.d);
                successCallback(parsedObject);
            }
        },
        error: function(error, status) {
            if (errorCallback) {
                errorCallback(error, status);
            }
        }
    });

这是实际的通话:

serverCall("POST", "SomePage.aspx", "GetSomething", inpuData, onSuccess, onError, true);

网络方法:

       [WebMethod(
          CacheDuration = 5,
          EnableSession = true)]
        public static string GetSomething(string user, string item)
{
// In the body i do the HTTPWebRequest that returns JSON
}

推荐答案

好,问题解决了.毕竟这不是安全问题,而是序列化问题.在网络方法中,我将接收到的对象(通过HttpWebRequest)存储在ASP.NET会话中.部署后,会话存储在db中,因此存在序列化问题.在我的计算机上,我使用了inproc会话,这就是为什么它可以在我的计算机上工作的原因.

Ok, problem is solved. It wasn't a security issue after all but a serialization issue. In the webmethod i stored the received object (via HttpWebRequest) in the ASP.NET Session. When deployed, the session is stored in a db and hence the serialization problem. On my machine I used inproc session, that is why it worked on my machine.

这篇关于jQuery Ajax调用失败,未授权401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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