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.