Javascript 试图通过AJAX针对我的WebAPI发出GET请求,仅在Firefox和Chrome中返回401
我只是对我的WebAPI方法发出了一个Javascript 试图通过AJAX针对我的WebAPI发出GET请求,仅在Firefox和Chrome中返回401,javascript,jquery,ajax,google-chrome,asp.net-web-api,Javascript,Jquery,Ajax,Google Chrome,Asp.net Web Api,我只是对我的WebAPI方法发出了一个AJAXGET请求——IE很好,但是Chrome和Firefox返回了一个错误 下面是我的jQuery客户端代码,它对我的WebAPI进行了AJAX调用: (function ($) { $.displayToastrNotifications = function (appId) { $.support.cors = true; var userId = ''; // get current use
AJAX
GET请求——IE很好,但是Chrome和Firefox返回了一个错误
下面是我的jQuery客户端代码,它对我的WebAPI进行了AJAX
调用:
(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中这样做-我在公司内部网上使用广告-有什么想法吗?请将此添加到您的web.config中
<location path="api">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
关闭Chrome的所有会话,并尝试使用命令“Chrome.exe--disable web security--user data dir”从cmd(windows)运行Chrome
如果操作正确,Chrome将发出警告标题:“您正在使用不受支持的命令行标志:--禁用web安全性。稳定性和安全性将受到影响。”您的IIS网站是否使用Windows安全性?它位于我公司的web服务器上-我不知道。您不能使用路由或web.config文件来保护MVC应用程序。保护MVC应用程序的唯一受支持的方法是使用具有[Authorize]属性的基类,然后使每个控制器类型子类都具有该基类-每MSDN@MikeMarks请看一看