Phonegap application does not request ajax with HTTP protocol, only with HTTP s

Asked

Viewed 194 times

1

I have an application created in Phonegap, however I can not make a request with it using the HTTP protocol, I can only make the request with protocol Https, someone has some idea of how to fix it ?

  • 1

    And it is not better to do in HTTPS?

1 answer

2


You have not reported on which system this is occurring whether it is on Android, iOS or both.

If this problem is occurring on Android, it may be being caused because of the recent change in security settings.

This change leaves unsafe protocols blocked by default.


Cases where you have access to manifest.xml

If your application uses the protocol HTTP you’ll need to put in your manifest.xml android:usesCleartextTraffic="true" on the tag <application>, leaving +/- like this:

<application 
    android:usesCleartextTraffic="true"
    .
    . 
    .

This property will make your application accept any HTTP connection.

If you want to be more specific and only allow some domains to use HTTP you will have to use this other property android:networkSecurityConfig="@xml/meu_arquivo_de_configuracao.
In this property you inform the xml file that contains the security information, Configuration example:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

See more about this by clicking here


Your case - Phonegap / Ordova

In case you are using the Phonegap, you will have to do something of the kind in your config.xml to configure the property android:usesCleartextTraffic="true"

<platform name="android">
  <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
      <application android:usesCleartextTraffic="true" />
  </edit-config>
</platform>

I’ll leave the link to a similar question by Soen, because you may need to change more things in your config.xml.


Other possible solutions

Alternative 1:

You can try to change the targetSdkVersion for an API version between 24 and 27.

Observing: In doing so it may also be necessary to change things such as implementation in your file build.Gradle.
Example of a implementation support library:

dependencies {
    implementation 'com.android.support:support-v4:27.0.0'
}

Observing: If the goal is to publish your app on Google Play to Alternative 1 is no longer valid because the Google Play began to require that the targetSdkVersion is at least in the Level 28 API from:

  • 01/08/2019 for new apps.
  • 01/11/2019 for app updates.


Alternative 2:

Since you are using AJAX, it may be that your server is not configured to accept CORS (Cross-Origin Resource Sharing) when responding to requests via HTTP. This setting depends on the server used by your service here at Sopt has some answers that can help set up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.