【问题标题】:Why is jQuery Ajax not working in Cordova iOS app为什么 jQuery Ajax 在 Cordova iOS 应用程序中不起作用
【发布时间】:2020-08-17 09:37:01
【问题描述】:

我正在构建一个使用 jQuery Ajax 将图像上传到我们的服务器的 Cordova 应用程序。上传在 Android 和 iOS 上都工作了一段时间,然后过了一段时间他们突然停止在 iOS 上工作。现在,在尝试访问网站几秒钟后,请求失败并且我收到一条错误消息,这并没有多大帮助。错误如下:

readyState: 0
status: 0
statusText: "error"

我的代码是:

    $.ajax("http://testapi.com/api", {
        data: submission,
        processData: false,
        contentType: false,
        method: "POST",
        // submission was successful
        success(data) {
          if (JSON.parse(data).success) {
            // backend validated and accepted submission
          } else {
            // backend rejected submission due to validation errors
          }
        },
        // submission failed
        // eslint-disable-next-line no-unused-vars
        error(xhr, text, error) {
          alert(
            JSON.stringify(xhr) + "  " + text + " " + JSON.stringify(error)
          );
        }
      });

我的内容安全政策,在 index.html 中是:

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

而 config.xml 是: `

    <config-file parent="NSCameraUsageDescription" target="*-Info.plist">
        <string>World Bee Count needs access to your camera and photo library</string>
    </config-file>
    <edit-config file="*-Info.plist" overwrite="true" target="NSLocationWhenInUseUsageDescription">
        <string>need location access to find things nearby</string>
    </edit-config>
    <edit-config file="*-Info.plist" overwrite="true" target="NSLocationAlwaysAndWhenInUseUsageDescription">
        <string>need location access to find things nearby</string>
    </edit-config>
    <string file="*-Info.plist" overwrite="true" target="NSLocationAlwaysUsageDescription">
        <string>need location access to find things nearby</string>
    </string>
    <config-file parent="NSPhotoLibraryUsageDescription" target="*-Info.plist">
        <string>World Bee Count needs access to your camera and photo library</string>
    </config-file>
    <preference name="StatusBarOverlaysWebView" value="false" />
    <preference name="StatusBarBackgroundColor" value="#2D81C1" />
    <preference name="Fullscreen" value="true" />
    <feature name="CDVWKWebViewEngine">
        <param name="ios-package" value="CDVWKWebViewEngine" />
    </feature>
    <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />
    <preference name="WKWebViewOnly" value="true" />
</platform>
<engine name="ios" spec="5.1.0" />
<plugin name="cordova-plugin-geolocation" />
<plugin name="cordova-plugin-device" />
<plugin name="cordova-plugin-file" />
<plugin name="cordova-plugin-filepath" />
<plugin name="cordova-plugin-whitelist" />
<plugin name="cordova-plugin-add-swift-support" />
<plugin name="com.subitolabs.android.cordova.galleryapi" spec="https://github.com/Collaborne/cordova-gallery-api.git"></plugin>
<plugin name="cordova.plugins.diagnostic" />
<plugin name="cordova-plugin-android-permissions" />
<plugin name="cordova-plugin-splashscreen" source="npm" />
<plugin name="cordova-plugin-camera-preview"></plugin>
<plugin name="cordova-plugin-wkwebview-engine" spec="~1.2.1" />
<plugin name="cordova-plugin-screen-orientation" spec="~3.0.2" />`

<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

我在 iOS 13 上运行它并使用 PhoneGap 构建

我的问题与此最相似 Ajax Not working in IOS 9.0 Cordova

我尝试根据question 更新我的 config.xml 但它似乎不起作用

任何帮助将不胜感激,我已经为此工作了几个小时,但似乎无处可去

【问题讨论】:

  • 遇到同样的问题,你解决了吗?
  • 是的,我会用代码添加我的解决方案

标签: jquery ios ajax cordova


【解决方案1】:

好的,我发现问题是因为最近从 UIWebView 更新到 WKWebView,这破坏了 Ajax 代码。如文档中所述,我将 cordova-plugin-wkwebview-file-xhr 插件添加到我的项目中。我还删除了 WKWebView 引擎插件的版本规范。我的 config.xml 被编辑为包含以下内容:

<plugin name="cordova-plugin-wkwebview-engine" />
<plugin name="cordova-plugin-wkwebview-file-xhr" spec="~2.1.4" />

<platform name="ios">
....
<preference name="AllowUntrustedCerts" value="on" />
<preference name="InterceptRemoteRequests" value="all" />
<preference name="NativeXHRLogging" value="full" />

我尝试了各种偏好组合,但只有这种组合有效。但是,可以禁用 NativeXHRLogging,因为它似乎并不重要。在此之后,一切又开始工作了

【讨论】:

  • 这似乎在 cordova-ios 6.1.0 下不起作用 - WKWebView 引擎已被集成并且添加 file-xhr 只会尝试引入 cordova-plugin-wkwebview-engine 插件,这会导致构建错误。有什么想法吗?
  • @AaronMason 我也面临同样的问题。添加 cordova-plugin-wkwebview-engine 插件后构建失败。
  • @subbu 如果您使用的是 cordova-ios 6.1.0,解决方案是删除该插件并在您的 config.xml 中添加这样的一行:&lt;preference name="scheme" value="myapp" /&gt; 然后让您的后端发送一个Access-Control-Allow-Origin 带有该 URL 方案而不是 http/s 的标头。
  • 我目前遇到了同样的问题。我正在使用cordova-ios 6.1.1,并且我已经删除了cordova-plugin-wkwebview-engine 插件,但ajax 仍然无法正常工作......我仍然得到预检响应不成功.. 加载资源失败。我已经尝试添加cordova-plugin-cors,但这也不起作用..还有其他建议吗?
【解决方案2】:

我在使用 Cordova IOS 6.1.1 构建时遇到了同样的问题。

我尝试了上述解决方案,但如前所述,使用 -engine 时遇到了构建错误。

我删除了 -engine 并且只使用了最新的 file-xhr 并且它工作正常。

只需使用以下插件,无论是否使用上述首选项。

<plugin name="cordova-plugin-wkwebview-file-xhr" source="npm" version="3.0.0" /> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多