1

I started an AngularJs App and to retrieve some data from database I'm using NodeJS (totally new to me), on the console of NodeJS it works and also typing the URL directly in the browser but when I try to get the information needed using http.get() in AngularJS using the same URL in the browser I get 404 not found. I figured it would be a cors problem so I added

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

Can anyone help me with that ? Am I right making separate apps for Angularjs in front-end and NodeJS in the Backend or should I assemble them in only one application ?

Thank you for your help

This is the AngularJS code:

$scope.keyLoad = function () {
    $http.get("localhost:8080/product")
         .success(function (response) {
             $scope.keys = response;
             console.log(response)
         })
};
$scope.keyLoad();
4
  • 1
    And your code is...? Commented May 2, 2016 at 12:24
  • unless your url is relative, you should specify the full url including protocol. try $http.get("http://localhost:8080/product") Commented May 2, 2016 at 12:33
  • And read this, why .then(...) is preferred than .success(...) Commented May 2, 2016 at 12:37
  • @Claies — CORS has nothing to do with 403 errors. You get those when the server refuses to give the client the data because the client isn't authenticated. CORS is used to bypass the Same Origin Policy which is when the browser denies access to JavaScript from one site that is trying to read data from another site. Commented May 2, 2016 at 12:44

2 Answers 2

1

I get 404 not found. I figured it would be a cors problem

If it says it is a 404 error then it is a 404 error and not a CORS problem.

Look at your code:

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

That URL is missing the scheme. It is a relative URL.

You are going to be requesting something like http://example.com/myapp/localhost:8080/product.

Put http:// or https:// in front of it.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use $http service.

For example:

$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.
});

Or

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

8 Comments

yes you are right , now it is printed in the console but can't use $scope.keys in an other place
what do you mean by in an other place?
I'm trying to use the result to draw a chart : var chart = c3.generate({ bindto: '#chart', data: { columns: [ ['Homme', $scope.keys], ['Femme', 88] ] } });
but $scope.keys is not recognised because it gives me an error : missing component
Where does this code goes to? Are they referring to the same $scope?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.