Im building an ionic v4 project with Django backend. When I run ionic serve -c --devapp
it deploys perfectly on browser, and I can access the backend, but when I try to test it on my phone using the DevApp, my request doesn’t reach the backend server.
This is my code:
import { HttpClient, HttpHeaders } from “@angular/common/http”;
import { Injectable } from ‘@angular/core’;
import { Storage } from “@ionic/storage”;const ROOT_ENDPOINT = ‘http://127.0.0.1:8000/api/’;
@Injectable({
providedIn: ‘root’
})
export class BackendAPIService {constructor(
private http: HttpClient,
private storage: Storage
) { }getHttpOptions(includeAuth: boolean = true){
let myDefaultHeaders = {
‘Content-Type’: ‘application/json’,
};
this.storage.get(‘authToken’).then((val)=>{
if (val && includeAuth){
myDefaultHeaders[‘Authorization’] = 'JWT ’ + String(val)
}
});
return { headers: new HttpHeaders(myDefaultHeaders) }
}login(userData:{}) {
const endpoint = String(ROOT_ENDPOINT) + ‘auth/’;
const options = this.getHttpOptions(false);
return this.http.post(endpoint, userData, options)
}
}
This is my backend CORS Configuration:
CORS_URLS_REGEX = r’^/api/.*$’ # Cors headers enabled
Request from
CORS_ORIGIN_WHITELIST = [
“http://192.168.0.11:8100”,
]
CORS_ORIGIN_ALLOW_ALL = Truefrom corsheaders.defaults import default_headers
CORS_ALLOW_HEADERS = list(default_headers) + [
‘my-custom-header’,
]
Any help will be appreciated! Thanks