簡體   English   中英

Angular使用Http請求不起作用

[英]Angular using Http request doesn't work

我正在嘗試使用Angular和一些API服務填充數據庫中的數據。

我的代碼:

        $http.get("/api/trips")
            .then(function (response) {
                angular.copy(response.data, vm.trips);
            }, function (error) {
                vm.errorMessage = "Failed to load data: " + error
            });

和我的API GET()

        [HttpGet("")]
        public IActionResult Get()
        {
                var results = _repository.GetTripsByUsername(this.User.Identity.Name);
                return Ok(Mapper.Map<IEnumerable<TripViewModel>>(results));
         }

沒有數據顯示。 我很確定錯誤會彈出,因為this.User.Identity.Name錯誤地傳遞了,但是我很困惑。

如果我將方法從GetTripsByUsername更改為GetAllTrips ,該方法選擇所有不帶過濾器的行程,則數據將正確顯示在頁面中。

它的功能可以自行運行,如果我通過PostMan使用它,它可以識別我的cookie並為我帶來正確的行程(在PostMan內部),但是在頁面上卻無法使用。

這不是因為您的角度應用程序未通過身份驗證,並且this.User.Identity.Name為null。
您需要使用以下角度代碼發送憑據:

$http.get("/api/trips", { withCredentials: true })
            .then(function (response) {
                angular.copy(response.data, vm.trips);
            }, function (error) {
                vm.errorMessage = "Failed to load data: " + error
            });

但這取決於您的身份驗證機制,此代碼不適用於Oauth或Basic身份驗證。 如果使用Oauth,則必須發送包含授權令牌的Authorization標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM