Phonegap can't load remote content - android

I've built an application in PhoneGap. I hand-code the application and upload it to phonegap build.
I am currently testing it only on Android.
My app uses Jquery Ajax to load some content from my server. This has worked PERFECTLY for the last 10 days. Today, it just stopped.
My application cannot load any content from any server anymore. This includes my Stylesheet (yes I prefer to host that on a server) and Google-hosted jquery. I resolved these files problems by just adding jquery.min.js and a client-side style.css file but the $.ajax calls, which I relied on so heavily cannot become client-side.
My config.xml file has always had
<access origin="*" />
The file in question has this code in it:
<body onLoad="loaded();"> .........
function loaded(){
alert("function called.");
$.support.cors=true;
}
The code which is now failing looks like this:
var request = $.ajax({
url: "http://myserver/somefile.php",
method: "POST",
data: { variable:value},
dataType: "html"
});
request.done(function( msg ) { alert(msg); }
Would anyone else care to take a swing?

Have you installed the cordova-whitelist-plugin and added a Content Security Policy meta tag in your app? If not, that may be the issue. This plugin is required for Cordova 4.0 or above (Android & iOS) to access any non file:// URLs. https://github.com/apache/cordova-plugin-whitelist#content-security-policy

Related

Phonegap / Cordova / jquery not connecting Internet

I have a small app to test:
https://github.com/vmbelizario/phonegap
its just a login form who connects to a PHP page and check if the login exist.
The side-server is the auth.php file.
The client-side is the js/auth.js who connect to http://myserver.com/auth.php (example) and do the login test.
If i upload in a apache + mysql server, works.
But in android, after put the credentials i got only "connecting..." and didnt worked.
I tested via adobe builder (build.phonegap.com/apps) build and install in my smartphone and via monaca.io android emulator.
Can someone check what is wrong and help me?
Thanks!
Make sure you have the Cordova Whitelist plugin installed.
cordova plugin add cordova-plugin-whitelist
This is required for Android and not on iOS or Browser.
Then check your config.xml for lines like these.
<access origin="*"/>
This will allow any network communication from your app. To be more safe you can change this to.
<access origin="YOUR_URL/*" />
This will allow network access only the the URL specified and any subdomains.
See the documentation here.
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/
I think you need to use the query string in the url. Try to concatenate the URL with your data string and pass it as a complete url in your ajax call. First try to hit the complete URL with static values in the browser, if it works then that's a solution for your problem. For example: http://myserver.com/auth.php?callback=?email=example#mail.com&password=password&login=

how to call external web services in phonegap application

I am new to phonegap. I want to call server side web service from phonegap application to get data. I have tried it on browser it working fine and also on Phonegap developer app for Android. Both are working fine. But if i make .APK from PhonegapBuild console and installed on android device, it doesn't work anymore.
I have used AJAX to call web service. I also tried other solutions related this but all are fail.
Solution 1:
Changed in config.xml
< access origin="*"/> with
< access origin="http://mywebserviceipaddress"/>
Solution 2: Changed AJAX call with .get() method. For this added image where i have added two methods for code.
Solution 3: Added
$.ajax({
...
crossDomain: true,
async: false,
cache : false,
...
});
in AJAX call
Here is my both code. Please tell me if missing any parameters.
enter image description here

PhoneGap Android Device Ready but no Ajax

I wonder if my problem is my ajax call is to http, not https. Must I call with https or can I call with http?
I am compiling with PhoneGap cloud cli-5.2.0 ( iOS 3.9.1 / Android 4.1.1 / Windows 3.8.1)
I compile my package, install it on my Android Samsung, it starts up, and my $.ajax errors
errorThrown:undefined
textStatus:error
data:{"readyState":4,"responseText":"","status":404,"statusText":"Not Found"}
Any suggestions/ideas?
I read the following:
jQuery Mobile + Phonegap on Android - no Ajax but its two years old and relates to an older version of phonegap.
My code requires no special magic - I do include phonegap.js but I do not use gps, camera, contacts etc etc
Using the code below, I do get my "deviceready" and "document ready" displayed. When I click on my login button (id='demologin') the function DemoLogin() is executed.
function onDeviceReady() {
$("#demologin").on("click", DemoLogin );
$("#demologin").after("<h3>deviceready</h3>");
return true;
}
$(document).ready(
function()
{
document.addEventListener("deviceready", onDeviceReady, false);
$("#demologin").after("<h3>document ready</h3>");
});
If I were to guess, the ajax "404" leads me to believe its calling a URL that does not exist hence why I wonder if Android expects me to call https instead of http. The problem I have with this is the code works on iOS so I would have thought PhoneGap magic would lead me to believe if it runs on one, it would run on the other.
All help appreciated!
I also use ajax on phonegap app and it work good. You can make call to http. I think the url you called does not exist or there may be connection problems like when data is switched off.
This is a old question but, possibly the solution will help someone who is googling it.
Add this code to your config.xml file:
<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
because new release of Cordova need whitelist plugin.

Phonegap + backbone fetch, not working due to cross domain

I'm currently developping a phonegap app with backbone but i'm not able to fetch my data from a cross domain website.
Testing the app on android gives me no results but testing it in chrome with "chromium-browser --disable-web-security" gives me a success response with the data I need.
Using phonegap I added the following lines of code:
config.xml
<access origin=".*"/>
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
json
[
{
"id":"918",
"merk":"Yamaha",
"type":"YZF R1"
...
Its a valid json file.
My backbone collectionName.fetch() doesn't work, and doing a simple ajax call isn't working either inside the app.
Is there any reason why I cannot fetch my data from the api in my phonegap android app!
I really could use some help
cheers!
From the file:// protocol there should be no limitations regarding same-origin, see here: http://en.wikipedia.org/wiki/Same_origin_policy#Corner_cases_and_exceptions
However it could be the server doesn't allow access to requests originating from file:// url's, check your server log. To doublecheck you could also access a publicly accessible webservice like Google Geocoding (see comment).
Edit: this is actually wrong...
This likely is because cross domain is only allowed within the same protocol. Since your phonegap app runs from file:// this introduces problems when connecting via http(s):// protocol.

Phonegap Android Filetransfer Error Code 3

I am working on mobile application for Android, which uses Dave Johnson's Facebook Connect Plugin (https://github.com/davejohnson/phoneg...), Camera and FileTransfer. Basically the user clicks on a button to Take a Photo ( The Phone Camera Starts, the user takes a photo ), then this photo is uploaded to a server. The problem is that first time everything is fine, but when I hit the button again to shoot a second photo, the photo can't be uploaded to a server and the returned error is from FileTransfer - Error Code 3.
I have set options.chunkedMode = false; This doesn't help.
I know that this is a connection error, I whitelisted the domain that I'm uploading the picture to in the /res/xml/cordova.xml file. I searched the web a lot to find a working solution there are other people complaining about this, but there is no working solution provided. I'm using Cordova 1.8.1 ( I also tested with 1.8.0, 1.7.0, 1.6.1, 1.5.0 ). I tested with newer versions of Phonegap for example 1.9.0 but can't build the project - the facebook plugin doesn't work with this version of Phonegap. I am testing using Emulator Android 4.1 (API 16). I tried different things to make it work while I was looking at the logs in details, but I couldn't make it work. I'm trying to find a solution, because it is very important, please help me solve this issue. Thank you in advance for you answers.
You can try either of these:
Set android:debuggable="true" in the tag of your AndroidManifest.xml file.
Set instead of just the * as it's recommended in the comments section as follows:
<!-- <access origin="https://example.com" /> allow any secure requests to example.com -->
<!-- <access origin="https://example.com" subdomains="true" /> such as above, but including subdomains, such as www -->
<!-- <access origin=".*"/> Allow all domains, suggested development use only -->
From; https://stackoverflow.com/a/12011782/610880
This is an unfixed bug in the phonegap library, since there were no bug fixes, i had to get it work by my work around (Basically reupload on every alternate failure):
ft.upload(path,
encodeURI("http://yourdomain.com/upload.php"),
function(result) {
alert("Uploaded");
},
function(error) {
ft.upload(path,
encodeURI("http://yourdomain.com/upload.php"),
function(result) {
alert("Uploaded");
},
function(error) {
alert("Error uploading image");
},
{ fileName: name, fileKey: "file", mimeType: "image/jpeg", chunkedMode: false }, true);
},
{ fileName: name, fileKey: "file", mimeType: "image/jpeg", chunkedMode: false }, true);

Categories

Resources