I have a problem and I don't know how fix it.
I has $http.get() in Ionic and when I do ionic serve it works perfect. But when I run ios/android or I see it in Ionic View nothing happens.
My Service:
.service('myService', function($rootScope, $http, $q) {
this.all = function() {
var deferred = $q.defer();
$http.get('xxxxxxxxxxxxxxx')
.success(function(data) {
deferred.resolve(data);
})
.error(function(data) {
alert(data);
deferred.reject(data);
});
return deferred.promise;
};
});
And in my Controller:
myService.all().then(function(data) {
$scope.data = data;
});
When I open in a browser it shows good, and in devices I get the alert with "null".
I do:
Ionic update
Install cordova-plugin-whitelist
Ionic build ios/android -> ionic upload / ionic run
add:
<access origin="*" subdomains="true"/>
<allow-navigation href="http://*/*"/>
in config.xml
But nothing work... Any idea?
Thanks so much.
I tried with every site and nothing. It's weird
Update: I add
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-eval' 'unsafe-inline'">
And the same thing
Related
I am trying to use webintents with a cordova app to be able to send images from another app to mine.
I am using cordova 5.1.1 and added the following plugins to my android platform project:
com.virtualartifacts.webintent 1.0.0 "WebIntent"
cordova-plugin-camera 1.1.0 "Camera"
cordova-plugin-console 1.0.0 "Console"
cordova-plugin-device 1.0.0 "Device"
cordova-plugin-file 2.0.0 "File"
cordova-plugin-file-transfer 1.1.0 "File Transfer"
cordova-plugin-whitelist 1.0.0 "Whitelist"
The index.html file for the project looks like:
<!DOCTYPE html>
<html>
<head>
<title>WebIntent Test</title>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<script src="cordova.js"></script>
<script src="js/webintent.js"></script>
<script>
function init() {
document.addEventListener("deviceready",deviceReady,false);
}
function deviceReady() {
console.log("App started. Device ready");
window.plugins.webintent.getExtra(window.plugins.webintent.EXTRA_STREAM,
function(data) {
console.log(data); // which never gets called
}, function(e) {
console.log(e);
// I simply get the message "Error"
});
}
</script>
</head>
<body onload="init()">
<h1>Demo WebIntent</h1>
</body>
</html>
so nothing really special here.
after searching the net for a while I found some information about the webintent plugin being buggy (as mentioned here on SO. So i found the patched version and double checked that the right code is in the WebIntent.java file, which is.
I also added the intent-filter tags in the AndroidManifest.xml file like so:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
When i installed the app on the device and try to share an Image my app shows up in the list of apps that are able to handle this sharing, and i also get the "App started..." text, so I know it gets called.
But no matter what kind of image i try i always reach the "error" part of the getExtra method and my console.log only shows "Error". Debugging is done directly on the device via GapDebug.
Is there anything I am missing or has anyone an idea on making my app getting images from other apps to work with?
Thanks in advance!
There is a bug in that Cordova plugin, use this fork instead
https://github.com/florentvaldelievre/virtualartifacts-webintent
so just remove the old one
cordova plugin rm com.virtualartifacts.webintent
and add the new one
cordova plugin add https://github.com/florentvaldelievre/virtualartifacts-webIntent.git
that should be it, although I'm didn't need to run js\webintent.js to get it to work.
This works when selecting a single image, arrays of streams doesn't seem to work yet.
I recently upgraded to Cordova 5.0 (and Cordova Android 4.0) and, since then, my app can no longer access external resources.
I still have <access origin="*" /> in config.xml (as before), and I still have <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.xml (as before), but ajax calls are rejected with no explanation (the "textStatus" param is "error", the "errorThrown" param is null, and xhr.state() returns "rejected").
I've verified that no request is hitting the server, so it appears it is being stopped by Android, but the log doesn't give any explanation as to why...
I can access the URL in question fine from the Android browser, just not from the app.
The ajax request is made via a call to Backbone.sync() of Backbone.js, which ultimately calls jquery's $.ajax(). I haven't changed anything about how the call is made... just upgraded cordova.
Are there new requirements/setup for network requests, in Cordova 5.0, or anything I need to do differently from previous Cordova versions?
Does anyone know of a way I can get more information as to why Android and/or Cordova is rejecting the request?
I tracked the culprit down to the android#4.0.0 cordova platform. It now requires the new cordova-plugin-whitelist plugin.
It can be installed with
cordova plugin add cordova-plugin-whitelist
or by adding
<plugin name="cordova-plugin-whitelist" spec="1" />
to config.xml, and then it is configured with
<allow-navigation href="*" />
in place of the old, <access origin="*" /> tag.
It's a little annoying that the log doesn't spit out the "whitelist rejection" error messages anymore when a problem like this comes up (that would have saved me a ton a time), but maybe that'll come later.
In Cordova 6.X you need to remove the builtin whitelist plugin and reinstall the new version of plugin.
cordova plugin remove cordova-plugin-whitelist
and re install the plugin
cordova plugin add cordova-plugin-whitelist
and then replace <allow-navigation href="*" /> to config.xml file instead of <access origin="*" />
in my case this trick worked.
Two things
Ensure the ajax url you are using allows cross origin requests
Are you passing relevant headers while making cross origin requests
To Read
http://enable-cors.org/
How to enable CORS in AngularJs
http://backbonetutorials.com/cross-domain-sessions/
if you are using cordova 6.x.x u need to uninstall builtin cordova-plugin-whitelist using
cordova plugin remove cordova-plugin-whitelist
and reinstall it by using
cordova plugin add cordova-plugin-whitelist
even if the issue persists relaunch command prompt and try it.
For me it started working after I removed from html file mata tag:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
I'm using this Google Analytics plugin for my Phonegap project https://github.com/phonegap-build/GAPlugin.
Everything works fine when I run it in the Android emulator. But when I use Phonegap remote build the plugin does not work. The plugin is listed along with other working plugins in the Phonegap app page.
Here's my code:
In config.xml
<gap:plugin name="com.adobe.plugins.gaplugin" version="2.3.1" source="pgb" />
In index.html
<script type="text/javascript" src="cordova.js"></
<script type="text/javascript" src="GAPlugin.js"></script>
<script type="text/javascript" src="js/index.js"></script>
In www/index.js after the device is ready (I censored the GA ID)
var gaPlugin = window.plugins.gaPlugin;
gaPlugin.init(function () {
log.innerHTML += 'ga ok.';
}, function (e) {
log.innerHTML += 'ga failed.' + e;
}, "UA-XXX-Y", 10);
gaPlugin.trackPage(function () {
log.innerHTML += 'tracked';
}, function () {
log.innerHTML += 'nope';
}, "test.com");
When I look into the build log I can't find any text "gaplugin". I don't know if it is even building the plugin or if I'm getting an error. I can't even use the debugger because the target is never listed.
Thanks for any help.
It turns out that if you're using hydrate, you have to re-install the APK instead of just reloading when you add a new plugin.
I have installed cordova (version 3.3) and built a sample project using these commands:
$ cordova create hello com.example.hello "HelloWorld"
$ cd hello
$ cordova platform add android
$ cordova build
and imported the project into Eclipse (according to http://cordova.apache.org/docs/en/3.3.0/guide_platforms_android_index.md.html#Android%20Platform%20Guide). I am able to successfully run the app from Eclipse by selecting Run As → Android Application.
Now I want to make use of cordova's notification ablities. I added the plugins (following this guide: http://cordova.apache.org/docs/en/3.3.0/cordova_notification_notification.md.html#Notification) with the commands:
$ cordova plugin add org.apache.cordova.dialogs
$ cordova plugin add org.apache.cordova.vibration
and when I type:
$ cordova plugin ls
it correctly lists the plugins I just added.
I return to Eclipse and paste the following code into assets/www/index.html (overwriting the existing code in index.html):
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Empty
}
// Show a custom alert
//
function showAlert() {
navigator.notification.alert(
'You are the winner!', // message
'Game Over', // title
'Done' // buttonName
);
}
// Beep three times
//
function playBeep() {
navigator.notification.beep(3);
}
// Vibrate for 2 seconds
//
function vibrate() {
navigator.notification.vibrate(2000);
}
</script>
</head>
<body>
<p>Show Alert</p>
<p>Play Beep</p>
<p>Vibrate</p>
</body>
</html>
When I deploy it to a device, it displays three links (Show Alert, Play Beep and Vibrate). When I press on these I expect the corresponding native notification to occur, but it doesn't. Instead I get the following error messages (displayed in LogCat):
Show alert: Uncaught ReferenceError: showAlert is not defined:45
Play Beep: Uncaught ReferenceError: playBeep is not defined:46
Vibrate: Uncaught ReferenceError: vibrate is not defined:47
How am I supposed to fix these errors?
Thanks in advance!
Have you tried to update the files and run everything from the www folder instead?
In your question you didn't mention updating the config.xml and AndroidManifest.xml files as noted in the API documentation. I'll copy them here for reference.
(in app/res/xml/config.xml)
<feature name="Notification">
<param name="android-package" value="org.apache.cordova.dialogs.Notification" />
</feature>
<feature name="Vibration">
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>
(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.VIBRATE" />
For further clarification see the answer to this question.
Should a phonegap plugin be declared in the config.xml file?
Cross-domain AJAX POST request works perfectly fine on web browsers including browsers on mobile phones, but doesn't work for native applications built using Phonegap
I have created a login form that users have to enter their login credentials, then they are verified by the server that is hosted on heroku and returns json {"success":true} if valid credentials are entered.
My Ajax script:
$.ajax({
type: "POST",
url: "http://domain.com/public/auth/app-login",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
dataType: "json",
data: {identity: <username from form>, password: <password from form>},
crossDomain: true,
cache: false,
success: function(data) {
obj = JSON.parse(data);
if (obj && obj.success === true) {
window.location.href = 'home.html';
}
},
error: function(e) {
alert('Error: ' + e.message);
}
});
Steps taken to resolve this issue:
Domain whitelisting - config.xml
<access origin="http://domain.com/public/auth/app-login" />
<access origin="*" />
Telling jQuery to allow cross-domain
$.support.cors = true;
OR
jQuery.support.cors = true;
Disable caching cache: false
Any help is appreciated.
Ok. If index.html in local then you can call ajax any hosts, not need enable CORS in client or server. You remove:
$.support.cors = true; OR jQuery.support.cors = true;
And:
<access origin="http://domain.com/public/auth/app-login" />
It redundant, only use:
<access origin="*" />
You need check and add in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
Add more permission if your app required. Finally, call ajax inside $(document).ready():
$.ajax({
type: "POST",
url: "http://domain.com/public/auth/app-login",
dataType: "json",
data: {identity: <username from form>, password: <password from form>},
success: function(data) {
obj = JSON.parse(data);
if (obj && obj.success === true) {
window.location.href = 'home.html';
}
},
error: function(e) {
alert('Error: ' + e.message);
}
});
If you are looking to resolve this issue then you may wish to make sure that the plugin is being properly included into the build process.
RESOURCE: \app\config.xml
<widget>
.... [lots of stuff] ....
<gap:plugin name="com.indigoway.cordova.whitelist.whitelistplugin" />
<access origin="http://*" />
....
</widget>
You may also wish to specify a version as that is recommended, and I do not specify one above. A good way to test if the plugin is included is to use the free cloud account provided at, https://build.phonegap.com/apps. If you build your project there you can check the plugins tab and make sure that the whitelist plugin is included.
I have read that you should only need this in the HEAD element of your HTML page, but I found as of the date of this post that I still needed to include the plugin.
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
If the plugin is not loaded you might get a "Not Found" error when using the $.ajax method for jQuery for the error string.
Some of the information on the Internet will tell you that the whitelist information is placed into the /www/res/ folder, but this appears to be outdated information. You may also find that <plugin... is used in some examples, but it appears that this may be an obsolete way?
Also, you may need:
RESOURCE: \app\config.xml
<widget>
...
<feature name="http://api.phonegap.com/1.0/network"/>
...
</widget>
use
JSON.stringify(data: {identity: <username from form>, password: <password from form>})
instead of
data: {identity: <username from form>, password: <password from form>}
I got success message when i changed my code like this.
Some time there is issue in your domain. In my case I resolved it by putting following code in my .htaccess file
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>