简体   繁体   中英

Not able to connect to backend over https from ionic android app

Unable to connect to backend over "https" on android app created through ionic CLI.

It works perfectly fine on browser, on debug mode on android phone. Just doesn't work in release mode. It even works over 'http' but not on 'https'.

And my SSL certificate is not self-signed. It is properly bought certificate and all SSL checkers say it is fine.

Tried all the solutions I could find on internet.

  1. Installed whitelist-plugin.
  2. Re-installed whitelist-plugin.
  3. Added Content-security-policy with "*" for default-src.

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; media-src *; script-src *;">

  1. Added <allow-intent href="*" /> <allow-navigation href="*" />

Looks like app itself blocking the request from going out. Nothing seems working. Please help.

The very simple solutions is, allow two ports in your API like

3001 for SSL / HTTPS and 3002 for HTTP.. This way your app works and your site runs perfectly in HTTPS

My code is as follows which worked perfectly:

 var express = require('express'); var DataController = require('./user/DataController'); var UserController = require('./user/UserController'); var db = require('./database/database-db'); var cors = require('cors'); var app = express(); app.use(cors()); app.use(function(req, res, next) { res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.setHeader('Access-Control-Allow-Methods', 'POST'); res.setHeader('Access-Control-Allow-Credentials', true); next(); }); app.use('/user', UserController); app.use('/data', DataController); app.get('/', function(req, res){ res.send("Welcome to the secure mobile and web development world"); }); // This settings are for HTTPS, SSL web applications. // var https = require("https"); // var fs = require("fs"); // var options = { // key: fs.readFileSync("/home/path/ssl/keys/key.key"), // cert: fs.readFileSync("/home/path/ssl/certs/crt.crt") // }; // https.createServer(options,app).listen(3001); // console.log('Welcome to the security world') // This settings are only for HTTP sites // var http = require("http"); // var fs = require("fs"); // http.createServer(app).listen(3001); // console.log('Welcome to the security') //This settings are for both HTTPS,HTTP SSL web applications. var https = require("https"); var http = require("http"); var fs = require("fs"); var options = { key: fs.readFileSync("/home/path/ssl/keys/key.key"), cert: fs.readFileSync("/home/path/ssl/keys/crt.crt") }; https.createServer(options,app).listen(3001); console.log('Welcome to the security world') http.createServer(app).listen(3002); console.log('Welcome to the proxy world') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM