[英]401 Unauthorized ajax call from angular node application calling Web Api 2 application
I have been able to make calls just fine to Web Api from an Angular / Node application when I use the $sce for an IFrame , but now I want to call Web Api from jquery ajax within the application.我已经能够当我使用$ SCE在iFrame从一个角/节点应用拨打电话蛮好的Web API,但是现在我想在应用程序内从jQuery的AJAX调用Web API。
When I try to call it I get当我尝试调用它时,我得到
401 Unauthorized 401 未授权
function getComments() {
$.ajax({
url: 'http://localhost:17308/Home/GetNewsComments?id=' + group,
type: 'Get',
data: { id: tipId },
success: function (data) {
$("#commentDetail").empty().append(data);
},
error: function () {
//alert("something seems wrong");
console.log("something is wrong");
}
});
};
FYI / BTW仅供参考/顺便说一句
I HAVE been able to make calls for IFrame with我已经能够调用 IFrame
$scope.setTipId = function (id) {
$scope.detailFrame = $sce.trustAsResourceUrl("http://localhost:17308/Home/GetNewsComments?id=" + id);
Can I do something similar for my jquery ajax calls from my controller?我可以为我的控制器的 jquery ajax 调用做类似的事情吗?
Update更新
I even tried the "angular way"我什至尝试了“角度方式”
$http.get('http://localhost:17308/CommentCount/').success(function (data) {
console.log('made it');
})
.error(function () {
console.log('error');
});
I still get the 401 error...我仍然收到 401 错误...
A call to a Web Api controller and loading an iFrame are fundamentally different things. 调用Web Api控制器和加载iFrame是根本不同的事情。
I suspect your controller method is actually requiring some sort of authorization. 我怀疑您的控制器方法实际上需要某种授权。 Decorate the Web Api Controller or Controller method with the attribute
[AllowAnonymous]
as seen below. 如下所示,使用属性
[AllowAnonymous]
装饰Web Api Controller或Controller方法。 If this is not an option, your problem is you do not have a valid ASP.NET session or are not adding the token in your http calls for authorization. 如果这不是一种选择,则您的问题是您没有有效的ASP.NET会话,或者没有在http授权中添加令牌。
[AllowAnonymous] //This allows all methods in the Controller to be accessed anonymously. Both are redundant in this case.
public class CommentCountController : ApiController
{
[HttpGet]
[AllowAnonymous] //This allows this method to be accessed anonymously . Both are redundant in this case.
public int Get()
{
return 1;
}
}
Well, this one is a bit painful, but this WILL work. 好吧,这有点痛苦,但这会起作用。 Note: you must now make ALL GET calls with jsonp , no more regular json dataType...
注意:您现在必须使用jsonp进行所有GET调用,不再需要常规的json dataType ...
Read and follow instructions from HERE: 阅读并遵循此处的指示:
http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started
Next, do all the painful Nuget install, uninstall and updates, I recall it being a bit painful with conflicts between the json formatmatter, web api, cors and newtonsoft json. 接下来,完成所有痛苦的Nuget安装,卸载和更新,我记得json格式问题,Web API,Cors和Newtonsoft json之间的冲突有点痛苦。
Persistence WILL pay off 坚持不懈将获得回报
Jquery call jQuery调用
$.ajax({
url: 'http://localhost:17308/Home/GetNewsComments?id=' + group,
type: 'Get',
data: { id: tipId },
contentType: 'application/json; charset=utf-8',
dataType: "jsonp",
callback: 'callbackFunc',
success: function (data) {
var json = $.parseJSON(data);
console.log(json);
},
error: function (xhr, status, error) {
console.log(xhr + '\n' + status + '\n' + error);
}
});
function callbackFunc(resultData) {
console.log(resultData);
}
I assume you paid attention and have Web Api , json formatter, CORS, along with other System.Web.Http updated and other dependencies. 我假设您已经注意了,并更新了Web Api,json格式程序,CORS以及其他System.Web.Http和其他依赖项。
So part of this is client side , but ultimately a lot HAS to do with server side with you using JSONP in web api 因此,这部分是客户端 ,但最终很多与服务器端有关的事情与您在Web API中使用JSONP有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.