简体   繁体   中英

Cannot call local web api from cordova android app

I am currently developing a cordova 4.3 android app in Visual Studio 2015 RC. It's an AngularJS app and I use $resource to send requests to an ASP.NET Web Api application running on a different port on the same computer. Debugging the app in Ripple works fine if I disable the Cross Domain Proxy but when I try to debug in the Visual Studio Android Emulator or on my Android device I get a not very helpful "Failed to load resource" as soon as try to reach the web api. The url looks correct and I suspect the problem has to do with either CORS or my HTTPS certificate or both? What I have tried so far is:

  1. Set <uses-permission android:name="android.permission.INTERNET" /> and android:debuggable="true" in the manifest file.
  2. Tried chrome://inspect/#devices to get more info about the problem but got the error message "Cannot load DevTools frontend from an untrusted origin".
  3. I am able to connect my android phone chrome web browser to the localhost web api using the very helpful tool sharpproxy [ https://www.codefromjames.com/wordpress/?p=97] but this approach unfortunately does not work with the app's webview.
  4. The ajax request does work when I point the app to my azure hosted live web api endpoint where I have a valid https certificate but that is not very convenient to use while developing the app.
  5. Made sure the config.xml-file contains <access origin="*" />
  6. My web api is configured with app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll)

I am completely new to cordova/hybrid app development and am surely missing some obvious thing? Any help or suggestion would be very appreciated!!

After having all sort of problems with Visual Studio 2015 I uninstalled it went back to Visual Studio 2013 Update 4 and now I can connect my android device to a local running web api using sharp proxy I mentioned and linked to above.

So to be clear:

  1. Use Visual Studio 2013 Update 4 to run your web api on for example the url https://localhost:44358/ .
  2. Start sharp proxy with external port 5000 and internal 44358.
  3. Make sure your android device is connected to the same wifi and instruct your app to use https://your-ip-address:5000 for all service calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM