Ajax error load Url from remote server in phonegap - ajax

I have been develop Phonegap android App. I put folder www in remote server, and in MainActivity(DroidGap) i loadUrl from server. I had config in Phonegap and App running in device ( load and show page from server). But problem when i send request by Ajax to server then it fail. It could not connect to any host. Please help me? Thanh you so much.
- MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("http://172.16.3.198:8080/ServerMail/index.html");
}
- Config.xml
<access origin="*"/>
<content src="http://172.16.3.198:8080/ServerMail/index.html" />

the app does not have the internet permission. you can edit AndroidManifest.xml, and something like below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>

Usually you have your 'site' files local to the app, not on a remote server, otherwise there is little point to Phonegap.
Here you might find that your URL is simply not reachable. A 172.16.x.x address is local to the network the server is hosted on, perhaps your phone is not on that network, especially if you are using 3G to connect.
Also, bare in mind that depending on the phone you use, you may be using an old version of webkit. As I've developed Phonegap apps, I have run into problems with jQuery 2 and older Android releases (2.1 for example). It seems to me that what you're doing is not a good use of Phonegap, and you might be better off using a shortcut instead.

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)

Push Notification in Windows App - How can I change the content of the Toast Message?

I have a Windows App developed with Cordova. I registered it with my Windows Developer Account and got my token etc. Now when I send a push message (toast) via Fiddler to the App, which is currently installed on an Windows Phone 8.1, I receive the Notification on the Phone just fine - but it only says 'New Notification'.
I used this xml for the message:
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
<wp:Toast>
<wp:Text1>Test Message</wp:Text1>
<wp:Text2>XXX</wp:Text2>
</wp:Toast>
</wp:Notification>
However, neither Text1 nor Text2 show up in my notification.
How can I implement the toast notification to actually use the content from the push notification?
Since my application is written in Angular JS, I can not use any C# documentations on the topic.
Any help with this problem would be much appreciated.
Thanks in advance!
It looks like you're using the MPNS format for toasts, but trying to push using WNS (I assume). Make sure you use the WNS toast format, documented here. Your toast should be formatted something like this:
<toast>
<visual>
<binding template="ToastText02">
<text id="1">Test Message</text>
<text id="2">XXX</text>
</binding>
</visual>
</toast>

How do I setup Thinktecture Identity server v3 beta 1-2 with ASP.NET Identity?

I have looked at all the docs for Thinktecture Identity server v3 and have not been able to figure out how to get started using ASP.NET identity.
Can someone please explain at a high level step by step from step 1 (i.e. cloning the git repo) to it's final state which is up and running with the Identity Manager as well. Essentially I just need to know how to set this up.
The videos I see on Vimeo seem out of date (and I may be wrong because I am new to this) because now there are several repositories and in the videos I think I saw the asp.net identity user service in the same solution in core.
I am trying to prototype this for my employer (AngularJS, Identity Server, OAuth 2.0, resource owner vs implicit flow, ) and am hoping to get this working as soon as possible.
Many thanks in advance!
Andrew
Have you checked Thinktecture.IdentityManager.AspNetIdentity solution? There is example how to configure it (see Host project):
public void Configuration(IAppBuilder app)
{
var factory = new Thinktecture.IdentityManager.Host.AspNetIdentityIdentityManagerFactory("AspId");
app.UseIdentityManager(new IdentityManagerConfiguration()
{
IdentityManagerFactory = factory.Create
});
}
In order to add this functionality to the clean project you just have to add necessary packages
<package id="Thinktecture.IdentityServer.v3" version="1.0.0-beta1" targetFramework="net45" />
<package id="Thinktecture.IdentityServer.v3.AspNetIdentity" version="1.0.0-beta1" targetFramework="net45" />
and configure it in the Startup. It's not necessary to clone git repo and compile it...

How do I prevent IIS 7.5 from caching symlink content?

I have set up IIS 7.5 to statically serve some files, and some of these files are actually symbolic links (created by mklink).
Even if I disabled both kernel and user caching, these files seems to be cached somehow by IIS. And IIS is still serving old versions after the files are modified.
To be sure that it is not caused by ASP.NET, I've created a dedicated unmanaged AppPool. I have also checked that these file are not cached by browsers.
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<caching enabled="false" enableKernelCache="false" />
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</configuration>
There are several people mentioning this problem:
http://forums.iis.net/t/1166077.aspx
http://forums.iis.net/t/1171204.aspx
Any hints how to solve this problem?
This problem drove me nuts for like a month a while back. You have to disable IIS caching in the registry, as far as I know this isn't documented anywhere for IIS 7 but instead is an old IIS 5 trick that still works. You can either turn the below into a .reg file and import it or you can just navigate to the section and add it manually. I recommend rebooting after changing this parameter, I'm not sure if IIS picks it up after just an iisreset.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\InetInfo\Parameters]
"DisableMemoryCache"=dword:1
I was previously able to fix this issue on IIS7 with Banin's fix. Since then, I have moved to Windows 10 with IIS 10, and suffered the same problem again. DisableMemoryCache did not help.
I then disabled kernel caching, and for now, that seems to fix the issue (I'm sorry for the Dutch in the screenshot):
Banin's solution worked for me. The issue was resolved after changing registry parameter and resetting IIS. The C# program below (you can use LINQPad to run it) will help you reproduce the issue:
using System.IO;
using System.Net;
void Main()
{
var virtualPath = "JunctionPoint/sample.js";
var physicalPath = $#"C:\IISROOT\JunctionPoint\{virtualPath}";
for (int i = 0; i < 100; i++) {
File.WriteAllText(physicalPath, i.ToString());
Console.Write(i + "=");
var client = new WebClient();
string html = client.DownloadString($"http://localhost/{virtualPath}");
Console.WriteLine(html);
if (i.ToString() != html) {
Console.WriteLine("Issue reproduced!!!");
}
}
}

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