如何使用AngularJS从NodeJS“获得”
我开始了一个AngularJs应用程序,并从数据库中检索一些数据我使用NodeJS(对我来说是全新的),在NodeJS的控制台上工作,也直接在浏览器中inputURL,但当我尝试获取所需的信息AngularJS中的http.get()
在浏览器中使用相同的URL我找不到404。 我想这将是一个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缺less该scheme。 这是一个相对的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);