【发布时间】:2014-01-29 23:53:44
【问题描述】:
我只是向我的 WebAPI 方法发出 AJAX GET 请求 - IE 很好,但 Chrome 和 Firefox 返回 401 Unauthorized 错误。
这是我的 jQuery 客户端代码,它使 AJAX 调用我的 WebAPI:
(function ($) {
$.displayToastrNotifications = function (appId) {
$.support.cors = true;
var userId = '';
// get current user's ID
$.ajax({
url: 'http://server/AppTools/API/Identity/GetCurrentlyLoggedInUserId',
type: 'GET',
dataType: 'text',
crossDomain: true,
success: function (data) {
userId = data;
// get all messages for the app
$.ajax({
url: 'http://server/AppToolsWS/api/BulletinBoard/GetMessagesForApp/' + appId,
type: 'GET',
dataType: 'json',
crossDomain: true,
success: function (data) {
DisplayToasterNotifications(data);
},
error: function (x, y, z) {
alert('getmessagesforapp: ' + x + '\n' + y + '\n' + z);
}
});
},
error: function (x, y, z) {
alert('getuserid: ' + x + '\n' + y + '\n' + z);
}
});
...
这是我的 WebAPI 方法:
[EnableCors("*", "*", "*")]
public class IdentityController : ApiController
{
[HttpGet]
public HttpResponseMessage GetCurrentlyLoggedInUserId()
{
var userid = string.Empty;
try
{
userid = HelperClasses.StringHelper.GetLogonUserID();
}
catch (Exception ex)
{
}
return this.Request.CreateResponse(HttpStatusCode.OK, userid, "text/plain");
}
}
我可以在任何浏览器中手动导航到此方法,它会成功返回数据。很奇怪,我不知道为什么它会在 Firefox 和 Chrome 中这样做——我在使用 AD 的公司内部网上——有什么想法吗?
【问题讨论】:
-
您的 IIS 网站是否使用 Windows 安全性?
-
它在我公司的网络服务器上 - 我不知道。
标签: javascript jquery ajax google-chrome asp.net-web-api