我使用Eclipse在Android上遇到XMLHttpRequest问题....
如果我在连接的设备上构建并运行应用程序,我没有错误,但是当我构建apk然后在设备上安装时,我收到错误,所以我错过了许可或其他什么。任何帮助表示赞赏。
var send = function() {
a = new XMLHttpRequest();
a.open((type.toLowerCase()==="post")?"POST":"GET",self._prepareUrl(url),true);//+'&username='+data.userid,true);
a.onreadystatechange = rsc;
a.timeout = 50000;
setTimeout(function () { /* vs. a.timeout */
if (a.readyState < 4) {
a.abort();
}
}, 50000);
a.ontimeout = to;
a.onerror = err;
a.onabort = errAbort;
a.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
a.send(resData);
};
错误功能是:
err = function(){
alert('send fail: stat: ' +a.status +' state: ' +a.readyState +' rtext: ' +a.responseText);
},
我得到的信息是:
status = 0 state = 4 没有回复文本
所以请求已中止,但为什么?我必须在下面的文件中遗漏一些东西,因为当我通过eclipse运行应用程序时它确实有效,默认情况下必须允许某些内容但是当设备上构建并安装apk时,我会在安装时获得允许权限信息屏幕当通过eclipse运行时。
清单是:
<?xml version='1.0' encoding='utf-8'?>
<manifest
android:versionCode="5"
android:versionName="1.3"
package="XXXXX"
android:windowSoftInputMode="adjustPan"
xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true"
/>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:hardwareAccelerated="true">
<activity android:name="CordovaApp" android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
和cofig.xml是:
<?xml version='1.0' encoding='utf-8'?>
<widget id="XXXXXXX" version="0.0.1" xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<access origin="*" subdomains="true" />
<log level="DEBUG"/>
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="true" />
<preference name="disallowOverscroll" value="true" />
<preference name="loadUrlTimeoutValue" value="60000" />
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="Device">
<param name="android-package" value="org.apache.cordova.device" />
</feature>
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.network-information" />
</feature>
<feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.splashscreen" />
</feature>
<feature name="Globalization">
<param name="android-package" value="org.apache.cordova.globalization" />
</feature>
答案 0 :(得分:0)
经过一番搜索,我找到了答案......
默认情况下,Android WebView不允许使用自签名SSL证书。当应用程序进行调试签名时,将忽略SSL错误,但如果app是与不受信任的服务的发布签名连接,则不会忽略SSL错误。
Phonegap Android app ajax requests to HTTPS fail with status 0
答案 1 :(得分:0)
如果英特尔XDK作为替代开发者平台是您的选择,那么 XMLHTTPRequest将起作用。
请在此处查看我的代码:XMLHTTPrequest and CrossWalk Android ( edit ..after i found a solution !) 将APK生成为HYBRID Legacy APP!
我也有这个问题,但它与人行横道有关。但只有没有人行横道的Android APK运行正常。我担心你会遇到同源政策问题。也许你应该仔细检查一下。
我的应用程序没有特定的清单。 XDK正在管理和编译这个。这部分对我来说仍然是一个黑盒子,因为我对这个东西很新......
此代码在我身边正常运作
//***************************************************************************************************
// WS THIS IS THE WS SECTION
//***************************************************************************************************
function XHRObject() {
var xhr;
xhr = new XMLHttpRequest();
xhr.onerror = function () {};
xhr.onstart = function () {};
xhr.success = function () {};
return xhr;
}
function getSphereParametersWSxhr() {
var url = "http://192.168.2.171/transporter.asmx/getVideoCubeParameters";
xhr = new XMLHttpRequest();
var params = "";
console.log(xhr);
alert("before open POST : " + xhr);
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// forbidden xhr.setRequestHeader("Content-length", params.length);
alert("after open POST : " + xhr);
try {
xhr.onreadystatechange = function () {
alert ("xhr.readyState == " + xhr.readyState + " xhr.status == " + xhr.status + " xhr.statusText: " + xhr.statusText + " xhr.responseText" + xhr.responseText );
if (xhr.readyState == 2 && xhr.status == 404) {
console.log("404 page not found: " + xhr);
alert("404 page not found: " + xhr);
}
if (xhr.readyState == 3) {
console.log("ready state 3: " + xhr.statusText + " " + xhr.status);
alert("ready state 3: " + xhr.statusText + " " + xhr.status);
}
if (xhr.readyState == 4 ) { //&& xhr.status == 200
console.log("ready state 4: " + xhr.statusText + " " + xhr.responseText);
alert("ready state 4: " + xhr.statusText + " " + xhr.responseText);
var erg1 = xhr.responseXML.getElementsByTagName("spherePictures")[0].textContent;
var stringList = erg1.split(";");
console.log(erg1);
alert("pictureList: " + erg1);
alert(xhr.responseText);
}
}
xhr.send(params);
} catch (e) {
console.log("XHR Post : " + e);
alert("XHR Post : " + e);
}
}