簡體   English   中英

如何使用 AngularJS 從 NodeJS“獲取”

[英]How to "get" from NodeJS with AngularJS

我啟動了一個 AngularJs 應用程序並從數據庫中檢索一些數據我正在使用 NodeJS(對我來說完全陌生),在 NodeJS 的控制台上它可以工作,並且還直接在瀏覽器中輸入 URL,但是當我嘗試獲取所需的信息時,使用AngularJS 中的http.get()在瀏覽器中使用相同的 URL 我得到 404 not found。 我認為這將是一個 cors 問題,所以我添加了

require('cors') in the nodeJS app and still doesn't work

任何人都可以幫助我嗎? 我是否正確地為前端的 Angularjs 和后端的 NodeJS 制作單獨的應用程序,還是應該只將它們組裝在一個應用程序中?

感謝您的幫助

這是 AngularJS 代碼:

$scope.keyLoad = function () {
    $http.get("localhost:8080/product")
         .success(function (response) {
             $scope.keys = response;
             console.log(response)
         })
};
$scope.keyLoad();

我得到 404 未找到。 我認為這將是一個 cors 問題

如果它說這是 404 錯誤,那么它是 404 錯誤而不是 CORS 問題。

看看你的代碼:

 $http.get("localhost:8080/product")

該 URL 缺少方案。 它是一個相對 URL。

您將請求類似http://example.com/myapp/localhost:8080/product

http://https://放在它前面。

您應該使用$http服務。

例如:

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

或者

$http.get('/someUrl', config).then(successCallback, errorCallback);

暫無
暫無

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

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