I try to use Angularjs http service to call my PHP API. Everything working fine in Browser. But, when I test it on my Android Phone, it will not work.
My Issue:
In Android PhoneGap Application, Whenever I trigger the Angularjs http service it will always trigger the function (error) even when the Mobile is connected with Internet.
I have try look this but their solution aren't working:
Angular $http.get() does not work in Phonegap DevApp
Cordova 5.3.1 Android app can't access the internet
I also have ensure the Phonegap config.xml has allowed Internet Permission Access with:
<access allows-arbitrary-loads-in-media="true" allows-arbitrary-loads-in-web-content="true" allows-local-networking="true" origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />
However it is still not Working.
My Question:
Is Angularjs http service is not able to work on PhoneGap Mobile Application but only Web Browser?
Aside from config.xml , is there any other configuration have to be made to allow PhoneGap Mobile Application to access the Internet?
Angularjs code:
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$http({
method: 'POST',
data: {
'email' : $scope.email,
'password' : $scope.password,
},
url: 'https://unsurfaced-cross.000webhostapp.com/PGLogin.php'
}).then(function (response){
$scope.users = response.data;
if(response.data[0]=="LOGIN"){
alert("Successful Login");
$window.location.href = './homepage.html';
}else{
alert("Login Failed. Incorrect Email or Password.");
}
},function (error){
alert("Please ensure You are connected to Internet.");
});
seem like actually I just didn't install the whitelist plugin, for my case the whitelist plugin didn't installed by Default.
Since I was using PhoneGap, instead of using command like cordova-plugin-whitelist#latest, cordova create, cordova plugin add cordova-plugin-whitelist or etc.
I should use: phonegap plugin add cordova-plugin-whitelist
After install the Whitelist Plugin, everything working fine.
So,
Angularjs http service is able to work on PhoneGap Mobile Application.
Aside from config.xml , we have to ensure that all necessary and required plugin was installed such as Whitelist plugin.
Related
I have just upgraded from Cordova v3.4 to latest 6.1.1. My whole android app is working just fine apart from ALL jquery Ajax calls here is a sample one:
var baseUrl = 'http://gazetkisklepowe.pl';
$.ajax({
url: baseUrl+"/api/get/favourites",
data: {
user_uuid: device.uuid
}
}).done(function(data) {
}).fail(function() {
alert('Error connecting. (100) '+baseUrl+"/api/get/favourites");
return null;
});
I have read this article: PhoneGap / Android WebView throws "Unknown chromium error: 0" but I haven't found a solution for me.
The message in the LogCat just displays: "unknown chromium error: 0"
My config.xml contains:
<access origin="*" />
<access origin="mailto:*" launch-external="yes"/>
SN: I would like just to inform that previously on Cordova v3.4 all Ajax requests worked just fine and there have not been any changes in the code since then.
UPDATE 10am 5/04/2016
I have added whitelist plugin and <allow-navigation href="data:*" /> to the config.xml, now I get:
object has no method 'error' at file ///android_asset/www/plugins/cordova-plugin-whitelist/whitelist.js:24
Line 24 in that js file is: console.error(msg); so from some reason console is not fully defined.
UPDATE 2pm 05/04/2016
I have created new cordova app in separate folder and copied my files over, that done the trick and have fixed everything.
In Cordova's 3.6.4 I was able to perform window.location = "http://somepage.com" to load a dynamic page inside the root/main webview.
Now in version 4.0.2, when I try to redirect the user through window.location or window.location.href = "http://destination.com" i get this popup to pick an app to launch the URL.
The most curious part is that Cordova iOS works just fine
Is there a reason for this?
Cordova 4.0.x introduces some new features and one of them is domain whitelist. This is a plugin that is added to the plugin list and installed allong with the application being compiled. This plugin can be configured through the config.xml normally located at the root of the cordova project. In my case, because I was interested in change the application main activity domain i added the following to my config
<!-- A wildcard can be used to whitelist the entire network,
over HTTP and HTTPS.
*NOT RECOMMENDED* -->
<allow-navigation href="*" />
Plugin page
Navigation-whitelist
I'm creating android application in Cordova witch consume a REST service using AngularJs,
I test this application first on browser by this URL
file:///C:/Users/Users/MyApps/project/platforms/android/assets/www/index.html#/demande/list but that stil give this Error :
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
http://10.0.2.2:8080/springrestprojet/rest/demande. Reason: CORS request failed.
I'm sing CORS filter for enabling access.
this is my code of service.js :
'use strict';
angular.module('workflowService', ['ngResource']).
factory('Demande', function ($resource) {
return $resource('http://10.0.2.2:8080\:8080/springrestprojet/rest/demande/:id', {}, {
'save': {method:'PUT',
headers: {'Content-Type': 'application/json',
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
'Access-Control-Request-Method':'*',
'Access-Control-Allow-Headers':'*',
'Accept': 'application/json'}},
});
});
Any help please ?
Thanks a lot.
Since a few months, the access origin="*" is not enough.
Try to install this plugin sudo cordova plugin add https://github.com/apache/cordova-plugin-whitelist
And then, in your config.xml add a line like that one
<allow-navigation href="*" />
The * isn't recommended, but it'll work for everything. You can specify the path like this http://yourWebSite.com/*
I suspect that in latest versions the plugin cordova-plugin-whitelist requires to configure the meta http-equiv="Content-Security-Policy" in each html file of your project.
With first version it only displayed warnings, but considering several questions seen here, maybe the behaviour has changed...
So you have two solutions :
remove the plugin cordova-plugin-whitelist and add instead cordova-plugin-legacy-whitelist then cordova will behave like with cordova 3.x where only access origin was checked (not recommended regarding security)
or you learn about Content-Security-Policy, for example by reading this page and update your htmls.
More info on Raymond Camden's blog or in the plugin's documentation.
Edit: now I remember, it's not that it changed in latest version it's that content security policy is checked on android starting with Kit-Kat, so a same app with cordova-plugin-whitelist and no CSP configured would work in android < 4.4 and fail in 4.4+.
So note to all cordova/phonegap developers, test your CSP code in kitkat or lolypop or you may have bad surprises!
I am trying to connect to server for fetching data using ajax call inside phonegap app.
But I am getting status 404 for every request I tried google.com, 10.0.2.2 (localhost on android).
Here are steps I followed for creating application
I created template application using phonegap command line as phonegap create my-app
Running application using phonegap run android ( This shows "device is ready" screen on emulator )
Added following code for making an Ajax call to index.js - receivedEvent function
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
console.log('rdy state - '+ xmlhttp.readyState);
console.log('status - '+ xmlhttp.status);
console.log('text status - '+ xmlhttp.statusText);
console.log('Response Text - '+ xmlhttp.responseText);
}
xmlhttp.open("GET","http://10.0.2.2/", true);
xmlhttp.send();
I can access http://10.0.2.2 from emulator browser.
Here is Enviornment I am using
Phonegap version - 3.4.0-0.19.8
Android version - 4.4.2
-- Vishal
Did you put an access token in your config.xml? Try to put
<access origin="*"/>
into your config.xml.
* gives access to all domain.
Look up http://docs.phonegap.com/en/3.4.0/guide_appdev_whitelist_index.md.html#Whitelist%20Guide for further information.
Despite you have:
<pre> <access origin="*"/> </pre> in config.xml
You need to add the whitelist to your phonegap(cordova) project:
phonegap plugin add cordova-plugin-whitelist
After this your app will no longer give 404.
Has anyone gotten the clipboard plugin to work with "PhoneGap Build"?
I can't get it to work. I'm currently useing phonegap 3.1.0
My app is for android and IOS. I've only tested the clipboard plugin on android so far, and it isn't working.
in my config.xml:
<gap:plugin name="com.verso.cordova.clipboard" />
in my javascript:
window.plugins.clipboard.copy('some text',function(){alert('success');},function(response){alert('error:' + response);});
window.plugins.clipboard.paste(function(text){alert('paste success:' + text);},function(response){alert('paste error:' + response);});
The way I have it above, the script runs, but for both the copy and paste calls, the error function is executed and the response is "Class not found".
I have tried the above call window.plugins.copy, cordova.plugins.copy
both of those just cause the script to abort.
phonegap is loading just fine and the deviceready function has already fired before I attempt the above calls.
Under the plugins tab on the phonegap build webpage for my app:
Installed 3rd Party Plugins
com.verso.cordova.clipboard 0.1.0
This appears to be unrelated to the copy/paste plugin. In phonegap 3.1 you have to reference all the phonegap plugins you want to use. In this case, I'm guessing you have a function that checks if the user has an internet connection? If so, you need to add to your config.xml