简体   繁体   English

PhoneGap / Android WebView引发“未知铬错误:0”

[英]PhoneGap / Android WebView throws “Unknown chromium error: 0”

I'm currently developing a project using PhoneGap and I need to do an ajax request to my local webserver which has already: 我目前正在使用PhoneGap开发一个项目,我需要向我的本地网络服务器发出一个ajax请求,该网站服务器已经:

PHP code PHP代码

header('Access-Control-Allow-Origin: *');  

Anyway, when I do an ajax request with jQuery on Android I get this error (in adb logcat): 无论如何,当我在Android上使用jQuery执行ajax请求时,我收到此错误(在adb logcat中):

D/chromium(23078): Unknown chromium error: 0

The Javascript code is: Javascript代码是:

$.ajax({url:"http://192.168.1.219/works/privati/folder/api.php/getlastmaginfo",dataType:"json",success:function(data)
{
    console.log("Finished loading by ajax");
    console.log(data);
}});

In Ripple Emulator works as expected, in Android nope. 在Ripple Emulator中按预期工作,在Android nope中。

Any suggestion? 有什么建议吗? Thank you for the help! 感谢您的帮助!

UPDATE 2013-08-21: 更新2013-08-21:

After some researches I came at the conclusion that $.ajax won't work with PhoneGap (don't know why, maybe a bug?). 经过一些研究后我得出的结论是$ .ajax不适用于PhoneGap(不知道为什么,也许是一个bug?)。 We must use $.get instead, but when I do a request with $.get I get Unknown chromium error: -6 I also read here that the problem is due to an Android's bug with the WebView URL mechanism. 我们必须使用$ .get,但是当我用$ .get做请求时,我得到了Unknown chromium error: -6我也在这里读到问题是由于Android的WebView URL机制的错误。

I'll continue my research until I find a good and working solution 我将继续我的研究,直到找到一个好的和有效的解决方案

UPDATE 2013-08-21 (2): Not even using this works... 更新2013-08-21(2):甚至没有使用这个作品......

var fileTransfer = new FileTransfer();

fileTransfer.download(
    "http://192.168.1.219/works/privati/qlipmag/api.php/getlastmaginfo",
    "json.json",
    function(entry) {
        console.log("OKAY");

    },
    function(error) {
        console.log(error);
    }
);

Error is at new FileTransfer(); 错误发生在new FileTransfer(); => Uncaught ReferenceError: FileTransfer is not defined => Uncaught ReferenceError: FileTransfer is not defined

UPDATE 2013-08-22: 更新2013-08-22:

It doens't work even by loading an external image: 即使加载外部图像它也不起作用:

<img src="http://externalhost.com/image.jpg"/>

Same error. 同样的错误。

In android manifest file I already setted permission: 在android清单文件中我已经设置了权限:

<uses-permission android:name="android.permission.INTERNET" />

I don't really know from where comes the problem... 我真的不知道问题出在哪里......

UPDATE 2013-08-27: I tried the same code on PhoneGap for iPhone (in iPhone Emulator) and the ajax request was successfull when I used an external website. 更新2013-08-27:我在PhoneGap for iPhone(在iPhone模拟器中)尝试了相同的代码,当我使用外部网站时,ajax请求是成功的。 Using an external website also on android doesn't get the same result. 在android上使用外部网站也没有得到相同的结果。 Same error. 同样的错误。

Anybody can see why? 谁能明白为什么? Access origin are setted fine... 访问来源设置得很好......

UPDATE 2013-08-27 (after 1 hour) 更新2013-08-27(1小时后)

ISSUE SOLVED 问题已解决

Actually PhoneGap doesn't allows ajax request to ip addresses, it only allows requests to whitelisted (in config.xml) domains. 实际上,PhoneGap不允许对IP地址进行ajax请求,它只允许请求列入白名单(在config.xml中)。 I used the production server (with domain) to test and it worked. 我使用生产服务器(带域)进行测试并且工作正常。

Thank you anyway guys. 无论如何,谢谢你们。 I hope this can help 我希望这可以提供帮助

为了澄清这种情况,万一有人偶然发现它,$ .ajax确实可以与PhoneGap / Cordova一起使用 - 但是当你发现..域名必须列入白名单。

As of Cordova 4 and above having only <access origin="*" /> within the config.xml doesn't work. 从Cordova 4及更高版本开始,config.xml中只有<access origin="*" />不起作用。 You need to use the cordova-plugin-whitelist. 您需要使用cordova-plugin-whitelist。

Once the plugin is installed you can use the following code within the config.xml to accomplish the same: 安装插件后,您可以使用config.xml中的以下代码来完成相同的操作:

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

Please note that in general it's no good practise to use the "*" als wildcard. 请注意,一般来说,使用“*”als通配符并不是一个好习惯。 Rather define the protocols and addresses specifically. 而是专门定义协议和地址。

For more information about how to use the cordova-plugin-whitelist please visit https://github.com/apache/cordova-plugin-whitelist 有关如何使用cordova-plugin-whitelist的更多信息,请访问https://github.com/apache/cordova-plugin-whitelist

Try disabling cache 尝试禁用缓存

$.ajax({
    cache: false,
    //your code
});

This error message is really vacuous. 此错误消息实际上是空的。 It mostly means that your app cannot find the resource on the file system. 这主要意味着您的应用无法在文件系统上找到资源。 So try disabling the cache, so it will always load the resource from the server. 因此,请尝试禁用缓存,因此它始终会从服务器加载资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM