简体   繁体   English

Cordova:HTTP GET在Android中不起作用

[英]Cordova: HTTP GET doesn't work in Android

I'm developing a mobile app on Cordova. 我正在科尔多瓦开发一个移动应用程序。 The main code communicates with a local server through HTTP GET, for example: 主要代码通过HTTP GET与本地服务器通信,例如:

    $.get( "http://192.168.2.6:8080/getStatus.sha",
        {},
        function(data) {
            alert ("checkIpAddress() success! Status: " + data.status);
        })
        .fail(function() {
            alert ("checkIpAddress() failed to get url \'" + url + "\'.");
        });

When I run that code on Mozilla Firefox, I get the server staus (and more data). 在Mozilla Firefox上运行该代码时,我得到了服务器状态(以及更多数据)。

But when I run the Cordova program on an Android device, I get the "checkIpAddress() failed" message. 但是,当我在Android设备上运行Cordova程序时,出现“ checkIpAddress()failed”消息。

The local server supports CORS. 本地服务器支持CORS。 The Crodova project's config.xml has these settings: Crodova项目的config.xml具有以下设置:

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

Any suggestions to fix the error? 有解决错误的建议吗? Thanks! 谢谢!

If you are using Cordova 5 and the device and server can see each other (on same wifi network) you likely have to enable this with the content security policy. 如果您使用的是Cordova 5,并且设备和服务器可以彼此看到(在同一wifi网络上),则可能必须使用内容安全策略启用此功能。 See Cordova Whitelist Plugin. 请参阅Cordova白名单插件。 You want to consider adding a meta tag to 您想考虑将meta标签添加到

Example configuration would look like: 配置示例如下所示:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

Depending on what else your app does or doesn't do you may not need all other options in the above, although some Ajax and templating frameworks will. 根据您的应用程序执行或不执行的其他操作,您可能不需要上面的所有其他选项,尽管有些Ajax和模板框架也会。 The above works for a Cordova 5 app using JQuery and Handlebars. 以上适用于使用JQuery和Handlebars的Cordova 5应用程序。

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

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