ionic CORS with Ajax call by third party library - ionic-framework

When making Ajax call by third party library (plain js library, Oracle MCS),
CORS errors shown.
Origin ionic://localhost is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load https://x.mobile.ocp.oraclecloud.com/mobile/platform/users/login due to access control checks
From my understanding, the webview engine thinks I'm browsing localhost for ionic pages,
ajax to else where is forbidden.
This is a common issue.
Below is the methods tried:
Add index content policy header
<meta http-equiv="Content-Security-Policy" content="default-src *;
style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'
'unsafe-eval'">
Whitelist localhost and remote address
<allow-navigation href="https://x.mobile.ocp.oraclecloud.com:443" />
<allow-navigation href="http://localhost:8080" />
Workaround:
1. use UIWebview
not ok as it is deprecated
2. use cordova-plugin-advanced-http
not applicable as the call is made by third party
3. use ionic local proxy in ionic.config.json to redirect
not applicable as the call is made by third party thus cannot modify the call
Sample test case:
let xhr = new XMLHttpRequest();
xhr.open("POST", "https://x.mobile.ocp.oraclecloud.com/mobile/platform/users/login");
xhr.send();
Hope to see advise on making Ajax call using native Ajax method.

I recommend use native http :
install plugin(ionic3) :
$ ionic cordova plugin add cordova-plugin-advanced-http
$ npm install --save #ionic-native/http#4
use post method ref
Alternative link. it seem perfect because it will work in browser(use angular client) and in device use native http.

Related

Ionic 4 + Electron not works

I am having problems to start a new Electron + Ionic project.
I have followed this tutorial:
https://www.9lessons.info/2018/10/ionic-electron-desktop-app.html
But when I run the project with the command:
npm run electron-serve
I get this errors on dev-tools console and the screen is white:
runtime-es2015.33b5e618b3baf74c7138.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
main-es2015.64da9645e917002baf25.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
polyfills-es2015.1b94da7ab13e05353bf7.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
/Users/myuser/ionic/Frontend/myproject/src/myproject/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/renderer/security-warnings.js:145 Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security
Policy set or a policy with "unsafe-eval" enabled. This exposes users of
this app to unnecessary security risks.
For more information and help, consult
https://electronjs.org/docs/tutorial/security.
This warning will not show up
once the app is packaged.
(anonymous) # /Users/myuser/ionic/Frontend/myproject/src/myproject/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/renderer/security-warnings.js:145
What would be doing wrong?
Easy, Just go to src/tsconfig.json -> change the line "target" : "es2015" into "target" : "es5" then re-run "npm run electron-serve" on the terminal and the app should work.
This works for Windows 10, Ionic 5.2.7, Electron 6.0.10
For someone with the same problem:
Is not really a Capacitor bug, but a build problem in your part related to the Angular 8 build settings not being compatible with electron.
Changing the target in tsconfig.json from es2015 to es5, or add
"browserslist": [
"> 5%"
]
in the package.json
Related
Solves the problem

I have an error when executing ionic serve

When I execute ionic serve, it shows the following errors:
Refused to load the image 'http://localhost:8100/favicon.ico' because
it violates the following Content Security Policy directive:
"default-src 'none'". Note that 'img-src' was not explicitly set, so
'default-src' is used as a fallback.
Failed to load resource: the server responded with a
status of 404 (Not Found)
How can I solve this?
I had the same error message.
Looking through the logs printed when doing
ionic serve
showed me a bunch of errors, all similar to this
[ng] ERROR in ./src/global.scss (./node_modules/#angular-devkit/build-angular/src/angular-cli-files/plugins/raw-css-loader.js!./node_modules/postcss-loader/src??embedded!./node_modules/sass-loader/lib/loader.js??ref--14-3!./src/global.scss)
[ng] Module build failed (from ./node_modules/sass-loader/lib/loader.js):
[ng] Error: Cannot find module 'node-sass'
[ng] Require stack:
This lead me to get the application up and running again by
npm install node-sass --save
The compilation process is sometimes spitting out rather confusing error messages from time to time, but with a little digging one can start guessing where to start poking
I had the same issue. I was able to solve it. I fixed the routing file, I had a route that couldn't load because another route was higher order.
Ionic include default favicon icon into index.html file.
<link rel="icon" type="image/png" href="assets/icon/favicon.png" />
You can put your favicon icon into "assets/icon" and replace href above.
I had this error and it was because I had included an import I wasn't actually using. In my example, I had accidently imported Console when using console.log.
I was getting error TS2307: Cannot find module 'console'. in the VS Code terminal, but my error in Chrome was "Refused to load the image 'http://localhost:8100/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
Failed to load resource: the server responded with a status of 404 (Not Found)"
So be aware that this error is not necessarily to do with the favicon.
I had same issue but solved using below solution.
<link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico" />
I used .ico file instead of .png file and thats works for me. try once.
I get same error multiple times,this error throw when u missed any small issue like wrong image path or forget to write import statement of any module/service/component.these error will shown in terminal when u running your project.After fixing these run your project
I had the exact same problem.
I advise you to take a look at your Terminal in VSCode (or the standard terminal if you launched it by there) and look for an ERROR message.
Most likely the problem isn't the favicon.ico blabla stuff but another element of your app, something in a .ts doc for example. Via Terminal you can seek for it in a better way than Chrome, that shows you this unrelated message.
I too faced same error .In my case i have done mistake in importing file
import { IUser } from 'src/app/shared/interfaces/IUser
Changed path like below is fixed error.
import { IUser } from './shared/interfaces/IUser
Cross check your import file. it might helps
I got the same error when I downloaded a new branch of the project, I just run npm install of the lastest components
I solved by created copy favicon.png from assets/icon/favicon.png to root project folder and rename it to favicon.ico , and also put it on assets/icon/favicon.ico
and also check this answer .
https://stackoverflow.com/a/63749758/8370334
Same issue here with ionic v5 and angular v10.
You may have this error when you create some error in the variables.scss file.
I was using adding a list of global variable that will be used across the app but I forgot to add a ,.
It didn't create an error while compiling...
The code was :
$color-settings: (
'primary': (
'base': #297bfd,
) // <-- Comma is missing here
'secondary': (
'base': #297bfd,
)
}
It will display the same error, I think, it breaks somehow the way ionic compile the scss.
I had the same issue after an upgrade. I solved the problem doing:
npm rebuild node-sass
Try to change the browser, I fixed this clicking on the url resource which opened firefox and then I've been able to test the app normally on Firefox

Image cannot be accessed from placehold.it in ionic with whitelist plugin

I am create very small demo in ionic with whitelist plugin. i take blank template and just take one image like below.
<img src="http://placehold.it/300x300">
This will work in samsung mobile which have old os version 2.3.6 and this is not working in mi which have lollipop. I try following things
i am trying to add meta tag in index.html file
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
chrome mobile inspect give following error
Failed to load resource: net::ERR_TIMED_OUT
http://prntscr.com/8kmdjj.
In config.xml file origin is * means we can access any url with our application. like calling web services or other url
<access origin="*"/>
But i cannot get any success. please help me for solving this. please

Facebook Connect with Phonegap Build not including cdv-plugin-fb-connect.js

I am using Phonegap Build and Hydration
My config does include the plugin
<gap:plugin name="com.phonegap.plugins.facebookconnect">
<param name="APP_ID" value="my_appId" />
<param name="APP_NAME" value="my_namespace" />
</gap:plugin>
On build.phonegap.com it is showing it my app under the plugins tab...
Installed 3rd Party Plugins
com.phonegap.plugins.facebookconnect 0.4.0
Installed PhoneGap Plugins
org.apache.cordova.camera 0.2.3
org.apache.cordova.console 0.2.3
org.apache.cordova.device 0.2.3
org.apache.cordova.dialogs 0.2.2
org.apache.cordova.geolocation 0.3.2
org.apache.cordova.globalization 0.2.2
org.apache.cordova.media-capture 0.2.3
org.apache.cordova.splashscreen 0.2.3
org.apache.cordova.vibration 0.3.3
I'm not specifying which version of PG to use so it is running 3.1.0
In my index.html file, in the head I have
<script src="phonegap.js"></script>
<script src="device.js"></script>
<script src="https://connect.facebook.net/en_US/all.js"></script>
<script src="cdv-plugin-fb-connect.js"></script>
<script src="facebook-js-sdk.js"></script>
Here is the part that I can't seem to find anyone else having a problem with:
In my deviceready callback I am testing for typeof CDV == 'undefined' to test and it's coming back as undefined. As far a I can tell is that cdv-plugin-fb-connect.js isn't getting included correctly.
Am I correct that cdv-plugin-fb-connect.js and facebook-js-sdk.js do not need to be copied into my www directory because they are included there by PGB?
Btw, I'm testing on Android Nexus5
<access origin="*" /> is in my config
I could never get CDV to work correctly on my Android emulator/phone.
However, I did get FacebookConnectPlugin.js to work, which is also included in the same official PhoneGap/Cordova Facebook plugin.
There are two version of this file -
One that you include in your root www/js folder
Another which is loaded by PhoneGap from the plugin folder after you deploy to platforms/android (I believe this is handled by cordova.js).
The second one overrides the JS SDK calls with native ones, so it degrades gracefully when you run in a browser.
For an example that actually seems to work, see the www folder in the develop branch.
e.g.
var getStatus = function () {
facebookConnectPlugin.getLoginStatus(
function (response) { alert(JSON.stringify(response)) },
function (response) { alert(JSON.stringify(response)) });
}

Confirmation was required to visit untrusted URL

Hi Im using eclipse-jee-galileo-win32 for development. I tried to install GWT using eclipse plugin for following link gwt - http://dl.google.com/eclipse/plugin/3.5. But it will displayed following error. How can i solve this problem.
[WARN] Confirmation was required to visit untrusted URL: 'http://gwt.google.com/missing-plugin
[WARN] reason: http://gwt.google.com/missing-plugin is not in the whitelist
whitelist:
blacklist:
To fix: add regex matching URL to -whitelist command line argument
Example: -whitelist=" ^http[:][/][/]gwt[.]google[.]com"
Example: -whitelist=" ^http[:][/][/]gwt[.]google[.]com"
To reject automatically: add regex matching URL to -blacklist command line argument
Example: -blacklist=" ^http[:][/][/]gwt[.]google[.]com"
Example: -blacklist=" ^http[:][/][/]gwt[.]google[.]com"
Unknown module requested 'missingplugin'; all active GWT modules must be specified in the command line arguments
This is caused due to your browser not having the necessary plugin to talk to GWT/Eclipse. Visit that page (http://gwt.google.com/missing-plugin) in your browser, install the plugin, restart the browser, retry the (local?) GWT page.