Home > other >  Cordova 10 unable to post ajax to http url
Cordova 10 unable to post ajax to http url

Time:12-19

I'm trying to send json data to an http url without success (I tried to send same data to an other https with success). I have this settings: config.xml

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

AndroidManifest.xml

android:usesCleartextTraffic="true"

HTML Header

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; frame-src *;"> 
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline' *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *">

<script>
  $.ajax({
    type: "GET",
    url: url,
    dataType: "jsonp",
    jsonp: 'callback',
    crossDomain: true,
    async: true,
    data: {
      id: results.rows.item(i).id,
      bolla: results.rows.item(i).bolla,
      anno: results.rows.item(i).anno,
      magazzino: results.rows.item(i).magazzino,
      articolo: results.rows.item(i).articolo,
      quantita: results.rows.item(i).quantita,
      term: terminale
    },
    success: function (data) {
      console.log(data)
    },
    error: function (xhr, textStatus, err) {
      alert("readyState: "   xhr.readyState);
      alert("responseText: "   xhr.responseText);
      alert("status: "   xhr.status);
      alert("text status: "   textStatus);
      alert("error: "   err);
    }
  });
</script>

If I use json it returns devicereadystate=0 and error if I use jsonp it returns devicereadystate=4 and error 404 (the url is correct if I paste to a browser it works)

CodePudding user response:

I believe this is because with cordova-android 10.0.x, the webview is now acting as a https page and you can't load/send to non secure origins while using https.

From the cordova docs

By default, the WebViewAssetLoader is enabled and allows apps to serve their content from a 'proper' origin. This will makes routing work easily for frameworks like Angular.

With no additional configurations, the app content is served from https://localhost/. You can configure the hostname by setting the preference option hostname.

   <preference name="hostname" value="localhost" />

The scheme, https, is not configurable by nature.

  • Related