Jump to content
Welcome to our new Citrix community!
  • 0

Using Cordova MicroVPN plugin for AJAX call on Android


Alvin Toh

Question

According to MAM SDK docs, it appears for Ajax requests we have to use cordova-plugin-fetch plugin.

 

Currently I am using JQuery .Ajax function therefore I have to switch to using the cordova-plugin-fetch plugin instead. Is my understanding correct? Otherwise how do I continue using JQuery .Ajax call (grateful for an example) with VPN connection ?

 

Thanks

Link to comment

3 answers to this question

Recommended Posts

  • 0

Could you please provide some details how the JQuery Ajax calls are made in your app? Are you using Cordova WebView (JS/HTML) or Android Native or if you are using any specific plugin? 

Cordova MVPN plugin enables Cordova WebView for tunneling as long as request remains within the same process and tunneling pre reqs are met. This is applicable for JQuery AJAX requests also that is initiated from the same Cordova WebView (JS/HTML). It doesn’t need cordova-fetch-plugin for that purpose. 

 

A simple example would look like below. No explicit calls needed to mvpn.

 

//index.js
var app = {
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },
    onDeviceReady: function() {
        $("button").click(function(){
            $.ajax({
                  url : 'https://api.weather.gov/',
                  type : 'GET',
                  dataType:'json',
                  success : function(data) {
                      alert('Data: '+data);
                      $("#div1").html(JSON.stringify(data));
                  },
                  error : function(request,error) {
                      alert("Request: "+JSON.stringify(request));
                  }
              });
          });
   },
};
app.initialize();

 
//index.html
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <title>Cordova Plugin CEM MVPN Test App</title>
</head>
<body style="text-align:center;">
    <br>
    <div id="div1"><h2>jQuery AJAX Change This Text</h2></div>
    <button>Click Here</button>
</body>
</html>

Link to comment
  • 0

Thanks for the clarification rajun.

 

I am indeed making JQuery AJAX requests directly within Cordova WebView (JS/HTML). Hence  I only need to install cordova-plugin-mvpn and be good to go, no code changes?

 

Nevertheless, can I still incorporate a check during initialization, something like:

 

        try {
            let tmp = mvpn;
            tmp = mvpnFetch;
            console.log('MVPN SDK detected.');
            return true;
        } catch (e) {
            console.error('No MVPN SDK');
            return false;
        } 

 

While I can see mvpnFetch defined in mvpn-fetch.js file, I am wondering where does the mvpn object come from. Am I missing another declaration?

 

A new question is if I am originally using JavaScript Fetch API, the setup is equally the same (just install cordova-plugin-mvpn and no code changes)?

 

Link to comment
  • 0

Yes, as long as Cordova Webview remains within the same process, cordova-plugin-mvpn should take care of tunneling requests.

 

Above code snippet should be okay to add. However it is not mandatory. It's just to make sure those objects are available.

 "mvpn" is mainly used for InAppBrowser, so instead of using cordova.InAppBrowser, you can directly use mvpn.InAppBrowser.

 

Regarding Javascript Fetch, it should okay also as long as it remains within the same web view.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...