为什么jQuery Ajax在Cordova iOS应用中不起作用 [英] Why is jQuery Ajax not working in Cordova iOS app

查看:149
本文介绍了为什么jQuery Ajax在Cordova iOS应用中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用jQuery Ajax将图像上传到我们服务器的Cordova应用程序.上载功能在Android和iOS上都运行了一段时间,然后过了一会儿突然停止在iOS上运行.现在,尝试访问网站几秒钟后,请求失败,并且我收到一条错误消息,对您的帮助不大.该错误如下所示:

I am building a Cordova app that uses jQuery Ajax to upload images to our server. The uploads were working for a while on both Android and iOS, then after a while they suddenly stopped working on iOS. Now, after a few seconds of trying to access a website, the request fails and I get an error message, which isn't much help. The error is like the following:

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中的我的内容安全策略"是:

My Content Security Policy, in index.html is:

<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是: `

And config.xml is: `

    <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构建

I am running this on iOS 13 and using PhoneGap build

我的问题与此最相似 Ajax无法在IOS 9.0 Cordova中使用

My problem is most similar to this Ajax Not working in IOS 9.0 Cordova

我尝试根据此问题更新我的config.xml. 但这似乎不起作用

I tried updating my config.xml according to this question But it doesn't seem to be working

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

Any help is would be very much appreciated, I've been working on this for hours and can't seem to get anywhere

推荐答案

好,我发现问题是由于最近从UIWebView到WKWebView的更新,这破坏了Ajax代码.我添加了 cordova-plugin-wkwebview-file-xhr 插件按照文档中的说明访问我的项目.我还删除了WKWebView引擎插件上的版本规范.我的config.xml已被编辑为包括以下内容:

OK, I found the problem was because of a recent update from UIWebView to WKWebView, which was breaking the Ajax code. I added the cordova-plugin-wkwebview-file-xhr plugin to my project as described in the documentation. I also removed the version spec on the WKWebView engine plugin. My config.xml was edited to include the following:

<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,因为这似乎无关紧要.此后,一切再次开始工作

I tried various combinations of the preferences, but only this combination worked. However, NativeXHRLogging could be disabled as it didn't seem to matter. After this everything started working once again

这篇关于为什么jQuery Ajax在Cordova iOS应用中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆