Search code examples
angularangular4-httpclient

Unable to post to a local server


I have a local Angular4 application that's trying to post to a local django server. The problem is that I'm getting an error in the http.post(), but I don't think it's caused by an issue with the CORS (I'm managing to consume this api using an android app, and I don't see any error message in the Angular-app about the Access-Control-Allow-Origin). Please find below my code:

// get token from the server
const body = {
    username: 'username',
    password: 'password'
};
this.http.post('http://website.loc/api/auth/login/', body).subscribe(
    res => {
        console.log('res:' + res);
    },
    (err: HttpErrorResponse) => {
        console.log('err');
        console.dir(err);
    }
);

Here is the error I'm getting (it seems that it's not able to read the url):

{headers: Object, status: 0, statusText: "Unknown Error", url: null, ok: false, name: "HttpErrorResponse", message: "Http failure response for (unknown url): 0 Unknown Error", error: ProgressEvent error}

Any clue what might cause these kind of issues?


Solution

  • You need to find actual problem, It may cross domain issue. In android, native app not using a browser, it direct call web api and cross domain comes in browser only.

    To find actual problem open developer tool and see console. If you see a message like cross domain or access denied. It's issue with cross domain. You have to configure cors in server. See in network activity for server response if there are not a cors issue. You can see actual service request and failure reason in network activity. In angular cache block you can not get actual problem which from configuration.

    This may also issue with calling request from https to non https url. Browser not give permission to call ajax request from https to non https url.

    To fix issue you have to find actual problem by developer console tool.