0
votes

I have a decoupled front-end and back-end. When I press submit button in front-end I would like to send POST http request to the server.

Code for submit:

$scope.onSave = function () {
        $http.post('http://localhost:3000/documents', { user: $scope.user, formData: $scope.formData }, function (response) {
            $scope.isSaved = true;
            console.log(response);
            if (response.success) {
                console.log("It has been successfully saved!")
            }
        });
}

Then, I get an error saying

XMLHttpRequest cannot load http://localhost:3000/documents. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3001' is therefore not allowed access. The response had HTTP status code 404.

What is an error here? I cannot really figure it out.

Another question: Let's say I'm logged in to this website. How do I get a USER info(like username or email) from the website. Do I access a URL header to get this info? When I send POST request, I need to send USER_ID as a param to successfully post data to the server.

1

1 Answers

0
votes

You should allow crossdomain access on serverside (CORS).