gpt4 book ai didi

javascript - 来自调用 Web Api 2 应用程序的 Angular 节点应用程序的 401 未经授权的 ajax 调用

转载 作者:行者123 更新时间:2023-11-30 16:35:46 25 4
gpt4 key购买 nike

当我将 $sce 用于IFrame,但现在我想在应用程序中从 jquery ajax 调用 Web Api。

当我尝试调用它时,我得到了

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");
}
});
};

仅供引用/顺便说一句

我已经能够调用 IFrame 了

$scope.setTipId = function (id) {
$scope.detailFrame = $sce.trustAsResourceUrl("http://localhost:17308/Home/GetNewsComments?id=" + id);

我可以对来自 Controller 的 jquery ajax 调用做类似的事情吗?

更新

我什至尝试过“Angular 方式”

$http.get('http://localhost:17308/CommentCount/').success(function (data) {
console.log('made it');
})
.error(function () {
console.log('error');
});

我仍然收到 401 错误...

最佳答案

调用 Web Api Controller 和加载 iFrame 是根本不同的事情。

我怀疑您的 Controller 方法实际上需要某种授权。使用属性 [AllowAnonymous] 装饰 Web Api Controller 或 Controller 方法,如下所示。如果这不是一个选项,则您的问题是您没有有效的 ASP.NET session ,或者没有在您的 http 授权调用中添加 token 。

[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;
}
}

关于javascript - 来自调用 Web Api 2 应用程序的 Angular 节点应用程序的 401 未经授权的 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32729778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com