Cordova android 应用程序无法访问我的服务器 API:Axios 网络错误

Cordova android app can't access to my Server API: Axios Network Error

提问人:AiAbdrahim 提问时间:10/26/2023 最后编辑:Olaf KockAiAbdrahim 更新时间:10/26/2023 访问量:38

问:

我有一个简单的cordova android应用程序,它使用本地服务器API来获取数据。 但是我无法从android访问此服务器。

<!DOCTYPE html>
<html>
  <head>
    <script src="cordova.js"></script>
    <title>Axios</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data:"/>
    <meta name="viewport" content="user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1,width=device-width,viewport-fit=cover" />
    <style>button {font-size: 100px;}</style>
    <script src="axios.js"></script>
  </head>
  <body>
    <button onclick="pingSrv()">Ping</button>

    <script>
      function pingSrv() {
        axios({
          method: "post",
          baseURL: "http://172.22.0.201/api/ping",
          headers: { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded" }
        })
          .then(({ data }) => alert(data))
          .catch((err) => alert(JSON.stringify(err)));
      }
    </script>
  </body>
</html>

config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>Sample Apache Cordova App</description>
    <author email="[email protected]" href="https://cordova.apache.org">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="*" />
    <access origin="*" />
    <allow-navigation href="*"/>

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

所以这是来自android应用程序的错误:

AxiosError: Network Error

请帮忙!

Android HTTP Cordova 公理

评论


答: 暂无答案