有一个asmx:
[WebService]
[ScriptService]
public class MyService : WebService {
[WebMethod]
public OperationResult Validate(string str) { }
}
有一个https://.../a.aspx,在这个页面中,我通过jQuery ajax调用web服务:
$.ajax({
url: '/Services/MyService.asmx/Validate',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: data,
success: function (data, textStatus, xhr) {
// xxx
}
});
请注意,a.aspx 使用HTTPS。 ajax 请求将得到 401 Unauthorized 响应。 如果 a.aspx 不使用 HTTPS,则它可以工作。
我正在使用一个名为“WebPageSecurity”的库,如果我使用它,就会发生401错误。如果我不用的话,就是直接输入https://.../a.aspx在浏览器中,然后它就可以工作了。 所以我猜问题出在WebPageSecurity上,我该如何解决它?谢谢。
最佳答案
最终,我找到了解决方案:忽略web.config中WebPageSecurity配置中的*.asmx文件:
<secureWebPages mode="On" ignoreHandlers="WithStandardExtensions" encryptedUri="xxx">
<files>
<add path="path/to/MyService.asmx/MyMethod" secure="Ignore" />
</files>
</secureWebPages>
关于jquery - 通过ajax调用asp.net asmx webservice返回401 Unauthorized错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090026/