Why is a POST request redirected to localhost using Angularjs and apache2?

Asked

Viewed 25 times

0

I am making a POST request to use an authentication API. The sample code works perfectly on the original server in this documentation

Follow the code used:

<!DOCTYPE html>
<html>
   <head>
      <title>
         AngularJs $http.post() Service Method Example
      </title>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script type="text/javascript">
         var app = angular.module('postserviceApp', []);
app.controller('postserviceCtrl', function($scope, $http) {
    $scope.name = null;
    $scope.age = null;
    $scope.adress = null;
    $scope.lblMsg = null;
    $scope.postdata = function(name, age, adress) {
        var data = {
            "email": "[email protected]",
            "password": "minhasenha"
        };
        //Call the services

        const serverApp = '​https://apiadmin.tindin.com.br/login';
        $http.post(serverApp, JSON.stringify(data)).then(function(response) {
            if (response.data)
                $scope.msg = "Post Data Submitted Successfully!";
        }, function(response) {
            $scope.msg = "Service not Exists";
            $scope.statusval = response.status;
            $scope.statustext = response.statusText;
            $scope.headers = response.headers();
        });
    };
});
      </script>
   </head>
   <body>
      <h2>AngularJS Http Post Method Example</h2>
      <div ng-app="postserviceApp" ng-controller="postserviceCtrl">
         <div>
            Name : <input ng-model="name" /><br/><br/>
            Age : <input ng-model="age" /><br/><br/>
            Adress : <input ng-model="adress" /><br/><br/>
            <input type="button" value="Send" ng-click="postdata(name, age, adress)" /> <br/><br/>
         </div>
         <p>Output Message : {{msg}}</p>
         <p>StatusCode: {{statusval}}</p>
         <p>Status: {{statustext}}</p>
         <p>Response Headers: {{headers}}</p>
      </div>
   </body>
</html>

However, when I run the same code using localhost with apache2, POST sending is redirected to localhost + API address. Acknowledging that the service does not exist. Dry the console message:

POST http://localhost/%C3%A2%E2%82%AC%E2%80%B9https://apiadmin.tindin.com.br/login 404 (Not Found)

Follow message from application:

Output Message : Service not Exists

I would like to know why this is happening, and how I can correct it.

  • 1

    http://localhost/%C3%A2%E2%82%AC%E2%80%B9https://apiadmin.tindin.com.br/login this chunk apparently has an unexpected concatenation. You may have noticed that the API example uses relative path, and yours is using absolute. I don’t remember much more how to use the Angularjs, but there must be some configuration for that. (yours specifically is version 1.4, which is more "old man")

  • Can test with that example? Maybe you just need to replace $http.post for $http({})

  • 1

    Your clue was correct! I was able to change the file destination by declaring the variable path outside the function at the beginning of the module. Instead of making const url = 'myAPI' ; http.post(url..... I made const url'myAPI'; module[].... Function(..... ) Would you like to do the honors and post as an answer? Just so no one else goes through this problem

  • then I’ll have to format it as in reply form. But if I can do that, it would be nice

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.