1

Could anyone tell me why the following statement does not send the post data to the designated URL?

login: function (username, password) {
    var deferred = $q.defer();

    var url = Config.serverUrl() + "/api-token-auth/";
    var data = {
        'username': username,
        'password': password
    };

    $http.post(url, data).then(function (responseData) {
        console.log(responseData);
        alert(url);
        alert(JSON.stringify(data));
        $localForage.setItem('shopper-user', responseData.data).then(function (user) {

            var token = "JWT " + user.token;
            $rootScope.token = token;
            $http.defaults.headers.common.Authorization = $rootScope.token;

            deferred.resolve(user);
        });

    }, function (error) {
        alert(url);
        alert(JSON.stringify(data));
        console.error(error);
        alert(JSON.stringify(error));
        deferred.reject(error);
    });

    return deferred.promise;
}

The URL is called on the server, but only go to the error state

Error:

{"data":null, "status":-1,"config":{"method":"POST"}}
5
  • what is your response code? 500? Commented Apr 6, 2017 at 21:52
  • Your server is not responding/request is timing out, try to ping the server and see what you get back Commented Apr 6, 2017 at 22:04
  • Possible duplicate of Angular $http status (-1) (not adding the close vote yet, will wait for feedback from OP) Commented Apr 6, 2017 at 23:11
  • The most common cause of status -1 is a Same Origin Policy violation. Modify the server to use CORS to allow cross-origin access. Commented Apr 6, 2017 at 23:45
  • Also there is no need to manufacture a promise with $defer as the $http service already returns a promise. See Is this a “Deferred Antipattern”?. Commented Apr 6, 2017 at 23:53

1 Answer 1

1

Client side code seems OK.

It's probably a CORS issue. Pleasure ensure server-side code responds 200 to OPTIONS request (made by Angular before POST request) and that response headers are properly set.

You can find more information on how to configure CORS here

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

Comments

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.