问题
AJAX calls fail when running the app in release mode on Android (works on iOS). This is the error (a warning really) I see in the Chrome Dev Tools:
Provisional headers are shown
Request URL:https://server.com/api/index.php?ticket=ST-111111-ABCDEFG-cas
Request Headers
Provisional headers are shown
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:file://
User-Agent:Mozilla/5.0 (Linux; Android 6.0; Google Nexus 5 - 6.0.0 - API 23 - 1080x1920 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.0.0 Mobile Safari/537.36
Query String Parametersview sourceview URL encoded
ticket:ST-111111-ABCDEFG-cas
Form Dataview sourceview URL encoded
request:{"param1":"val1","param2":"val2"}
Everything works fine on Debug mode. My last year release build for Android is still working fine also. So it may be related to the new version of Cordova. Here is the tools versions I have:
- Node version: v4.4.3
- Cordova version: 6.2.0
- cordova-android@5.1.1
- Android SDK up to date
I did set these settings in the config.xml:
<allow-navigation href="*" />
<access origin="*" />
<allow-intent href="*" />
I am using the whitelist plugin:
<plugin name="cordova-plugin-whitelist" spec="1" />
I also set this meta in the index.html file:
<meta http-equiv="Content-Security-Policy" content="default-src *; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
Is there other Cordova settings I forgot to set?
FYI, I have removed and re-addd the Android platform to my project, updated all my plugins but I still get the same Ajax error.
Please help.
回答1:
Had exactly the same problem. $.ajax works fine on debug version. Release Version fails with status code 0. Issue was a week SSL certificate. I changed the https to http and then it worked in release too. See:
https://forum.ionicframework.com/t/cordova-plugin-whitelist-problem-only-in-release/43705/3
回答2:
From API side api developers need to allow cross origin headers.
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: CUSTOM_HEADERS);
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS);
Also when request come from cordova or using ajax, then there ajax request two call: first call is for options, in which api returns only allowed headers & methods after that second is actual api call which call api.
来源:https://stackoverflow.com/questions/37784267/ajax-calls-fail-when-running-android-cordova-app-in-release-mode