PhoneGap Android Device Ready but no Ajax - android

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.

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 can't load remote content

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

PhoneGap database working on emulator, but not on device

Im just learning PhoneGap and I have some problems conserning the databases...
SO I want to connect to my localhost databse and simply display the items from the table.. With the ripple emulator everything works, but when I install the app on my device the data is not displayed, but im not getting any errors either.
So I am using the PhoneGap Desktop App. The server is running on http://88.216.170.246:3000
So in my ajax I do this:
$(document).on('click', '.show', function (e) {
$.ajax({
type:"GET",
url:"http://88.216.170.246/Test/www/getData.php",
success: function(result) {
if (result) {
$(".show_data").html(result);
}
else {
alert("error");
}
}
});
});
});
This renders everything perfectly on the emulator, but on my phone, nothing happens... Does the url not work? Maybe I cant access localhost on my device?
Please read:
Top Mistakes by Developers new to Cordova/Phonegap
https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md
READ #1, #4, #5, & #10
#4 reads
In the code, did not listen for the 'deviceready' event.
This is listed MULTIPLE times in the documentation, and is include in every example where it is appropriate. It is still missed. Brian Ford - an Angular developer, points to the section of documentation we need.
This is a very important event that every Cordova application should use.
Cordova consists of two code bases: native and JavaScript. While the native code is loading, a custom loading image is displayed. However, JavaScript is only loaded once the DOM loads. This means your web application could, potentially, call a Cordova JavaScript function before it is loaded.
The Cordova deviceready event fires once Cordova has fully loaded. After the device has fired, you can safely make calls to Cordova function.
Please read the FAQ and follow the links. You have made many bad assumptions.
Best of Luck
Jesse

Installation of plugin in phonegap app

I want to create an phonegap app using this plugin. I have done this steps to create and add plugin :
1:I am creatin this app using phonegap cli :-
phonegap create GetPhone --id "com.phone" --name "GetPhoneApp"
2: Going into the project and buid
cd GetPhone
phonegap local build android
3:Installing plugin
phonegap local plugin add https://github.com/macdonst/TelephoneNumberPlugin
4:moving telephonenumber.js to www folder
5: adding
<script type="text/javascript" charset="utf-8" src="telephonenumber.js"></script>
into index.html then added below ondeviceready :-
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function(error) {
alert("error = " + error.code);
});
6: Added
<plugin name="TelephoneNumber" value="com.simonmacdonald.cordova.plugins.TelephoneNumber"/>
into config.xml in www directory.
7: Build
phonegap local build android
8: Uploaded into build.phonegap.com and created .apk file.
Then when i am running this app on android device it does not alert anything. Is there any wrong step i have taken. I dont know much more about phone gap. After googling i have created this. I have seen this tutorial also to add this plugin. When i am build it locally it does not show any error. But it is also not running.
Please help me find the error.
First of all I would suggest you get acquainted with standard tools, especially adb. These will allow you to provide more information, find out what really is going on.
Skimming through the guide is also helpful, so you can be sure you know what you're doing and why you're doing it. Look at step-by-step debugging of Android code as well.
I would also encourage you to build a Cordova application from sources without using the fancy tools to get the hang of what goes where and how all the components come together. Your source tree is confusing and you seem to have used a mixture of Cordova 3.1 and Cordova 2.9, and even Phonegap? You only need to include cordova.js and your script (js/script.js in this case), nothing else at the moment.
Having said that, on to the major issue.
Your call to get returns an error. Why? Take a quick look at the source code of the plugin:
https://github.com/macdonst/TelephoneNumberPlugin/blob/master/src/com/simonmacdonald/cordova/plugins/TelephoneNumber.java
The plugin retrieves the TelephonyManager and tries to call the getLine1Number. And as you can see result != null is false which drops through to returning a PluginResult.Status.ERROR, which is why you get your error alert box.
This is the core issue at hand. TelephonyManager returning a null for your request. Why? After searching around you might stumble on:
Retrieve phone Number in android
TelephonyManager.getLine1Number() failing?
Reading device phone number throws NULLPointerException
So, navigate over to Settings > About Phone > Status on your device and look at the "My phone number" field. Is it "Unknown"? Tough luck then. My SIM doesn't store the number, so I got the same results as you did.
Another thing to note, is that at least on Android 4.4, the method returns an empty string "" instead of a null, so when testing on my Android 2.3.7 device I got a null and thus got the error branch. But on my Android 4.4.2 device I got a "" and got the success branch, but the number was empty, obviously.
You can set the number on the emulator by using this pretty advanced guide if you'd like to try. So what can you do? Probably nothing by using the API, not even the Android system itself can get your number if it's not stored on the SIM card. Asking the user or sending an SMS to discover the number via a web service may be an option as well.
Step number 5 above will not work unless the code is called after the 'deviceready' event is triggered.
you will need some sort of call to know when the document is loaded, you can do either:
<body onload="onLoad()">
<script>
function onLoad() {
document.addEventListener('deviceready', deviceReady, false);
}
</script>
OR (w/ jquery):
<script>
$(document).ready(function () {
document.addEventListener('deviceready', deviceReady, false);
}
</script>
and then create this function:
function deviceReady() {
var telephoneNumber = cordova.require("cordova/plugin/telephonenumber");
telephoneNumber.get(function(result) {
alert("result = " + result);
}, function(error) {
alert("error = " + error.code);
});
}
This will ensure that your document has loaded and that cordova is ready to call to.

Categories

Resources