I have simple rest api hosted on here, (django rest). And it’s has CORS enabled in settings file.
Then on my ionic app i tried to access the API like following,
// this method uses angular http client library
loadData() {
return this.http.get(this.URL).pipe().subscribe(
(res) => {alert(res[1].email); this.var2 = res[1].email; },
(err) => { alert(err);}
)
}
// this method uses ionic native http library
async getDataNativeHttp() {
const loading = await this.loadingCtrl.create();
await loading.present();
from(this.nativeHttp.get(this.URL, { }, {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'})).pipe(
finalize(() => loading.dismiss())
).subscribe(
data => { this.var2 = data[0].email,
alert(data[0].email);
},
err => { console.log('Native Call error: ', err); }
);
}
then i have tested it with following
ionic serve
ionic cordova run browser
ionic cordova emulate android
in 1 way loadData()
method returns CORS error on web console, when web browser has CORS plugin enabled it shows expected results and getDataNativeHttp()
method is not working since it’s native.
2 way loadData()
method returns the expected results whether CORS plugin enabled or disabled, and getDataNativeHttp()
method just recieve Status code:301
and no data,
3rd way both methods not working, and i’m using Android Studio’s emulator to test, and logcat console show following
when loadData()
method called
2019-08-16 18:43:38.620 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:43:39.183 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:43:39.209 7416-7416/io.ionic.starter I/Choreographer: Skipped 33 frames! The application may be doing too much work on its main thread.
2019-08-16 18:43:39.673 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:43:40.583 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:43:40.599 7416-7416/io.ionic.starter D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
2019-08-16 18:43:40.600 7416-7416/io.ionic.starter D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
2019-08-16 18:43:40.604 7416-7416/io.ionic.starter I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load https://finalprojectjr.pythonanywhere.com/pass/list_passenger. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.", source: http://localhost/home/tab2 (0)
2019-08-16 18:43:40.723 7416-7416/io.ionic.starter I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread.
2019-08-16 18:43:40.727 7416-7493/io.ionic.starter D/EGL_emulation: eglMakeCurrent: 0xa7365d80: ver 3 0 (tinfo 0x96f3d2f0)
when getDataNativeHttp()
method called,
2019-08-16 18:46:38.509 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:38.898 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:39.310 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:39.707 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:39.724 7416-7416/io.ionic.starter D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
2019-08-16 18:46:39.724 7416-7416/io.ionic.starter D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
2019-08-16 18:46:40.136 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:40.556 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:40.945 7416-7427/io.ionic.starter I/art: Background partial concurrent mark sweep GC freed 61(2808B) AllocSpace objects, 1(12MB) LOS objects, 10% free, 33MB/37MB, paused 181us total 138.338ms
2019-08-16 18:46:40.973 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:41.391 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:41.805 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:41.952 7416-7427/io.ionic.starter I/art: Background partial concurrent mark sweep GC freed 595(27KB) AllocSpace objects, 2(25MB) LOS objects, 10% free, 33MB/37MB, paused 314us total 128.628ms
2019-08-16 18:46:42.223 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:42.619 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:43.037 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:43.465 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:43.870 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:43.886 7416-7416/io.ionic.starter I/chromium: [INFO:CONSOLE(45196)] "ERROR", source: http://localhost/vendor-es5.js (45196)
2019-08-16 18:46:44.281 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:44.677 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
2019-08-16 18:46:45.032 7416-7416/io.ionic.starter W/art: Attempt to remove non-JNI local reference, dumping thread
Any suggestions on what’s wrong here ?