Jquery ajax doesn't work on phonegap app - ajax

I have been working on a phonegap app and it was working perfectly fine until today when the Ajax post request would get stuck loading and the success or failure callbacks are not being called but when I try it on the browser it works perfectly fine.
Has anyone came across this problem and if so how did you manage to fix it?
Thank you in advance
EDIT:
This is my first hybrid app and i am not using desktop app. for now I am only targeting iOS and the target version is 3 and I am using build

The newer version of the phonegap automatically blocks all the requests made to any server. You need to follow the instructions to make it work.
Add the "Cordova Whitelist Plugin" in your config.xml
<gap:plugin name="cordova-plugin-whitelist" spec="1.1.0" />
Or simply
<plugin name="cordova-plugin-whitelist" spec="1.1.0" />
After that try putting the following in your config.xml
<content src="index.html" />
above mentioned is the path to your HTML file inside the source directory.
<access origin="*" />
will allow you to make request to any of the server using ajax. If you want to restrict your app to just a single domain then use something like this.
<access origin="http://yourdomain.com" />
At the end, put the following intents
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />

For Android users who have this (I know that's not the original question):
I had the same problem, and spent a lot of time flailing unsuccessfully with the various whitelist directives as described above. Actually, my problem was different: my .apk file had not been correctly signed. Bizarrely, my Android phone did not kick it out at the point of trying to install the .apk: it simply failed as soon as I attempted to make an ajax call. I found the problem because Google Play refused to accept the upload.
I'm using Adobe PhoneGap Build to create the .apk: the problem was fixed when I added my Android keystore to the build (and disabled debug, which I don't think was relevant, but I'm not sure).

its work for me :
in path platforms/android update file AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Related

Cordova : How to diagnose ajax not working for UWP (windows store) application

I have a simple Cordova application, where when built, and running as a Windows UWP application, has ajax calls are somehow being blocked my work network.
I have asked this many times before, but thought would try to reword, as have never got any solutions.
The application ajax calls work fine on my home machine, or whats seems to be most other networks. When it works, I can see all the output in Wireshark
When on my work machine, connected to our work Network, I see absolutely nothing in Wireshark. If I point to a server running on localhost, the app does work (but I see nothing in Wireshark, but perhaps this is because it is localhost)
If I run the app outside of the UWP container, ie I just run on the same machine, on the same network, but via the desktop browser (as you do for Cordova to debugging), it also works fine.
So it appears to be blocked before it even gets to the Network, as we see nothing in Wireshark at all, so this rules out host not reachable, CORS etc as it hasn't even been sent to the server.
I can run this in debug via Visual Studio, and I run the following test code...
document.addEventListener('deviceready', callUrl, false);
function callUrl() {
console.log('callUrl');
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
console.log(xhr.readyState);
if (xhr.readyState === DONE) {
if (xhr.status === OK)
console.log(xhr.responseText); // 'This is the returned text.'
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
}
xhr.open('GET', 'https://httpbin.org/get');
xhr.send(null);
};
onreadystatechangeis called twice, with xhr.readyState first 2 and then 4.
status is always 0.
How can I diagnose what is blocking this?? I have absolutely no idea. This is something low level, but how to see what? I have also looked through Windows Event logs, but can find nothing.
The Ajax call just returns 0 with a blank description. Our Network administrator just says there is something wrong with my app (I have tried multiple Cordova apps, including just basic test apps)
Thanks in advance for any help.
[EDIT1]
In response to the comment from #Xavier, all I have in my AppxManifest.xml (that I extracted from my built .appxupload file) is
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
There is some documentation on the capabilities here, where we have the following (right at the bottom of the page)..
The following capabilities are unavailable when deploying your Remote Mode application to the Windows Store:
Enterprise Authentication (enterpriseAuthentication)
Shared User Certificates (sharedUserCertificates)
Documents Library (documentsLibrary)
Music Library (musicLibrary)
Pictures Library (picturesLibrary)
Videos Library (videosLibrary)
Removable Storage (removableStorage)
Internet client/server (internetClientServer) - note that internetClient is still permitted
Private network client/server (privateNetworkClientServer)
I am not sure were we even set these in Visual Studio (config.xml), but also the above seems to be saying some of these can't be used?
Should the internetClient be enough (I only want to call out to a server as a client, not act as a server).
Also, this seems to be enough to work on most Networks except for my Work..
[EDIT2]
In response to the reply on the CSP...
There is a discussion on this here. The test Cordova app I created in Visual Studio does have this :
<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 *">
Surprisingly, I don't see this in my Ionic index.html, but perhaps this is because it is using the white list plugin?
My Ionic application has the following in the config.xml
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-navigation href="http://localhost:8080/*" />
....
<plugin name="cordova-plugin-whitelist" spec="^1.3.1" />
It would be great if the ajax even had some error saying it was something to do with CSP if that is what it is, but we just get nothing.
Once again, it is weird that the problem only occurs on my work network (either on cable or our WIFI). If I run the exact machine (e.g. a Surface tablet)over another connection (e.g. tether it to my phones cell), then all works as expected. So it must be some setting to do with the network (or firewall maybe), which also I find strange as surely I would at least then see something in Wireshark.
Would be great to be able to "debug into" the ajax call, and see where it is failing.
[EDIT 3]
After reading one of the comments, I used fiddler to see if I could see anything, which I do...
It even reports 200 (which is not correct), my request still fails.
According to this, you need to request the privateNetworkClientServer capability in order to communicate with a local network.
Note that this capability only works if you app is configured for local mode (your app will be rejected from the store if using this capability in remote mode).
To enable local mode, you need to set <preference name="WindowsDefaultUriPrefix" value="ms-appx://" /> in your cordova config.xml.
Note that local mode might lead to other issues, as you cannot use e.g. inline scripts in local mode (CSP violation)

Parse - Installation table not setting deviceToken nor pushType

I had an app working on Parse, with the notifications working fine. I changed app packaging and I create a new app on Parse.
With the new app, deviceToken and pushType columns remain always empty on table _Installation, so pushes doesn't work even if sent from Parse web page, and a new entry is generated as application is launched.
I've updated parse keys on my java code and on my cloud code.
Someone has some idea what I may have missed or what may happen so the same code has different behaviour in different apps with equivalent configurations?
If I update one of the installation with pushType="gcm" and "deviceToken" the one I had in the other app, this device receives notifications.
Thank you
okay, I had a similar issue. both those columns were empty.
this is mainly due to the manifest issue.
your permissions seem okay because you are getting the notification and also able to reg in the parse data base.
so the problem should be in the <receiver> tags there should only be 2 of them like mine.
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: If you change the package name of this sample app,
change "com.parse.tutorials.pushnotifications" in the lines
below to match the new package name.
-->
<category android:name="com.example.ifis" />
</intent-filter>
</receiver>
if you have any receiver like "com.google.android.gcm.GCMBroadcastReceiver" pls remove and also one <service android:name="com.parse.PushService" />
Enable parse logging:
Parse.setLogLevel(Parse.LOG_LEVEL_VERBOSE);
In my case I was missing GCM permissions
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="my.package.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />

Parse.com how to enable client pushes

I'm targeting specific users when sending Parse notifications, using their email address. The send seems to work okay, but no message is received. What do I need to change to receive the message?
In my Parse control panel it shows the attempted pushes but with the error message that "client-initiated pushes are not enabled". I really prefer initiating the pushes from the client. How do I enable that? I keep finding documentation that says to enable client pushes but I can't find how to do it.
Here is my setup:
Parse.initialize(this, "...xxxAPP_ID", "...xxxCLIENT_KEY");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseObject parseObject = new ParseObject("defaultObject");
parseObject.put("email", sEmail)
parseObject.saveInBackground();
The actually sending is done here:
ParseQuery pQuery = ParseInstallation.getQuery();
pQuery.whereEqualTo("email", ToUser);
ParsePush parsePush = new ParsePush();
parsePush.setQuery(pQuery);
parsePush.sendMessageInBackground("You have pictures waiting from " + sEmail, pQuery);
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature"
android:name="com.pictureplay.permission.C2D_MESSAGE" />
<uses-permission android:name="com.pictureplay.permission.C2D_MESSAGE" />
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.pictureplay" />
</intent-filter>
</receiver>
Update: I have narrowed the problem down to push notifications being received unless the service is not running. I thought the Parse service was supposed to load when the phone restarts, but this is not the case here. Is there a way to get the Parse service for my app to load when the phone reboots?
Update2:
The solution was to create a separate class (called "App" in my case) which initializes Parse. If the Parse service gets shut down when the app closes, the service restarts itself within minutes (automatically, I did no special setup for this) to be able to receive incoming push messages. These are the lines of code used in "App" onCreate:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "data1", "data2"); //supply your own
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
Have you enable Sending notifications from Client. Its a setting in Parse.com.
Go to settings ->App Permissions ->Allow client class creation. Set it to ON>
In your parse application Go to settings > Push > Client Push Enabled > set to Yes
The question Is there a way to get the Parse service for my app to load when the phone reboots? : Yes you can receive the BOOT_COMPLETE event if you have added the following permission on your manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
In the new Parse DashBoard: go to settings---> push ---> push notification settings---> client enabled push --- >set to "yes"

Rhomobile: Custom URL Schemes

I'm writing a Rhomobile application targeted at iPhone and Android.
I need to create a custom url scheme, so that i can create urls that look like test://some-params that will launch my program and will pass it the params.
As far as I understand this is done in build.yml through the BundleURLScheme parameter, and then System.get_start_params() to get those parameters.
However, this works on the iPhone only as far as I understand.
Is there any way to make this work on Android too?
Thanks alot!
OKay, I've found the answer myself, in case anybody needs this too:
Create an extension to the application as explained here:
http://docs.rhomobile.com/rhodes/extensions#generating-a-native-extension-template
Add an android_manifest_changes file, as decribed in the above link.
In that file add the following lines:
<manifest xmlns:android='http://schemas.android.com/apk/res/android'
android:versionName='1.0' package='com.rhomobile.webbrowserpoc'
android:versionCode='10000' android:installLocation='auto'>
<application android:name='com.rhomobile.rhodes.RhodesApplication'
android:label='#string/app_name' android:icon='#drawable/icon'
android:debuggable='true'>
<activity android:name='com.rhomobile.rhodes.RhodesActivity'
android:label='#string/app_name' android:launchMode='singleTask'
android:configChanges='orientation|keyboardHidden'
android:screenOrientation='unspecified'>
<intent-filter>
<action android:name='android.intent.action.VIEW' />
<category android:name='android.intent.category.BROWSABLE' />
<category android:name='android.intent.category.DEFAULT' />
<data android:pathPrefix='' android:scheme=''
android:host='' />
</intent-filter>
</activity>
</application>
Only the <data android:pathPrefix='' android:scheme='' android:host='' /> line should be filled in with correct properties.

How does it happen Azure web role entry point and .aspx page handler are run in different processes?

I'm playing with this Azure web role sample. It contains a class derived from RoleEntryPoint and a .aspx page that contains a button click handler.
I test it in Azure Emulator. I put the following code (taken from here)
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
in both role OnStart() and the button click handler. When role OnStart() is invoked it happens to run in WaIISHost.exe under MachineName\\MyLogin account and when button handler code is invoked it happens to run in w3wp.exe under MachineName\\NETWORK SERVICE account. That's surprising.
Why are these pieces of code from the same role project run inside different processes and under different accounts? Can I change that?
David is correct. In addition to that, you can turn off this behavior and run everything in the hostable web core (as it worked before SDK 1.4). You just need to comment out the "Sites" section in the services definition like in the example below:
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="aExpense.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="aExpense" vmsize="Medium">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="HttpsIn" endpointName="HttpsIn" />
</Bindings>
</Site>
</Sites>
<ConfigurationSettings>
<Setting name="DiagnosticsConnectionString" />
<Setting name="DataConnectionString" />
<Setting name="allowInsecureRemoteEndpoints" />
</ConfigurationSettings>
With Windows Azure v1.3 and beyond, a Web Role takes advantage of the full IIS, rather than Hosted Web Core. IIS runs in a separate appdomain.
See this blog post from the Windows Azure team for the gory details.

Resources