UNPKG

4.95 kBMarkdownView Raw
1# A solution to CORS problem of Ionic and WKWebView
2
3[![travis build](https://img.shields.io/travis/sneas/ionic-native-http-connection-backend.svg?style=flat-square&maxAge=2592000)](https://travis-ci.org/sneas/ionic-native-http-connection-backend)
4[![version](https://img.shields.io/npm/v/ionic-native-http-connection-backend.svg?style=flat-square)](http://npm.im/ionic-native-http-connection-backend)
5[![MIT License](https://img.shields.io/npm/l/component-library.svg?style=flat-square)](http://opensource.org/licenses/MIT)
6
7## Motivation
8
9Even though there is a way to solve CORS issue without changing server's response header by using [Cordova HTTP plugin](https://ionicframework.com/docs/native/http/), the problem is it works only on device and doesn't provide all the power of Angular's `Http` and `HttpClient` services.
10
11This project's been born as a solution to CORS problem allowing to use Angular's `Http` and `HttpClient` services in both environments: browser and device.
12
13## Installation
14
15```bash
16npm install --save @ionic-native/http ionic-native-http-connection-backend
17ionic cordova plugin add cordova-plugin-advanced-http
18```
19
20## Usage
21
22Angular > 4.3 provides two modules for HTTP requests:
23
24- `HttpClientModule` (imports from `@angular/common/http`).
25- Deprecated `HttpModule` (imports from `@angular/http`).
26
27`ionic-native-http-connection-backend` supports both modules.
28
29### HttpClientModule (@angular/common/http)
30
31Add `NativeHttpModule` and `NativeHttpFallback` into the application's module
32
33```typescript
34import { NgModule } from '@angular/core';
35import { HttpBackend, HttpXhrBackend } from '@angular/common/http';
36import { NativeHttpModule, NativeHttpBackend, NativeHttpFallback } from 'ionic-native-http-connection-backend';
37import { Platform } from 'ionic-angular';
38
39@NgModule({
40 declarations: [],
41 imports: [
42 NativeHttpModule
43 ],
44 bootstrap: [],
45 entryComponents: [],
46 providers: [
47 {provide: HttpBackend, useClass: NativeHttpFallback, deps: [Platform, NativeHttpBackend, HttpXhrBackend]},
48 ],
49})
50export class AppModule {
51}
52```
53
54### Deprecated HttpModule (@angular/http)
55
56Add `NativeHttpModuleD` and `NativeHttpFallbackD` into the application's module
57
58```typescript
59import { NgModule } from '@angular/core';
60import { NativeHttpFallbackD, NativeHttpModuleD } from 'ionic-native-http-connection-backend';
61import { RequestOptions, Http } from '@angular/http';
62
63@NgModule({
64 declarations: [],
65 imports: [
66 NativeHttpModuleD
67 ],
68 bootstrap: [],
69 entryComponents: [],
70 providers: [
71 {provide: Http, useClass: Http, deps: [NativeHttpFallbackD, RequestOptions]}
72 ],
73})
74export class AppModule {
75}
76```
77
78## Contributing
79
80Contributing guidelines could be found in [CONTRIBUTING.md](CONTRIBUTING.md)
81
82## Troubleshooting
83
84### (HttpModule, @angular/http) I followed installation and usage instructions but still receive CORS issue on app start
85
86Wrap the first request with `Platform.ready()`. The code will resemble the listing below.
87
88```typescript
89this.platform.ready().then(() => {
90 this.http.get('url')
91 // subscribe logic goes here
92});
93```
94
95`ionic-native-http-connection-backend` uses `cordova-plugin-advanced-http` to perform HTTP requests. There is a chance plugin could be initialized in few seconds after app has started. Using `ionic-native-http-connection-backend` before `cordova-plugin-advanced-http` has been initialized falls it back to `XmlHttpRequest` usage.
96
97The above instruction relates to requests performed on app start only. There is no need to wrap all the HTTP requests with `Platform.ready`.
98
99This is a temporary solution. The issue has been created. Check https://github.com/sneas/ionic-native-http-connection-backend/issues/14 for it's status.
100
101### I followed the installation and usage instructions but still experience CORS issues on device
102
103Sometimes `cordova-plugin-advanced-http` displays as installed but in fact it's not which forces the lib to fall back to XMLHttpRequest usage.
104
105To make sure `cordova-plugin-advanced-http` is installed correctly and works properly change any project's page class to correspond the code below. The example below is for `HomePage`.
106
107```typescript
108import { Platform } from 'ionic-angular';
109import { HTTP } from '@ionic-native/http';
110
111export class HomePage {
112 constructor(
113 private platform: Platform,
114 private nativeHttp: HTTP,
115 ) {
116 this.platform.ready().then(() => {
117 this.nativeHttp.post('https://httpbin.org/post', {a: 'b'}, {})
118 .then(() => {
119 console.log('cordova-plugin-advanced-http is installed properly');
120 });
121 });
122 }
123}
124```
125
126Run the app on device and watch console output. Absence of the message `cordova-plugin-advanced-http is installed properly` in console output says `cordova-plugin-advanced-http` has not been installed or recognized. Try to reinstall it and give it another chance.
\No newline at end of file