【发布时间】:2018-06-07 06:03:17
【问题描述】:
我正在使用 Framework7 制作 Cordova 应用程序。 我想对本地主机上的文件进行 ajax 调用。 当我在我的网络浏览器上点击 localhost 的 ajax URL 时,我得到了我想要的完美结果。 但是当我尝试通过 Android Studio 模拟器中的 Cordova 应用程序进行 ajax 调用时,它失败了。
我猜这与 Cordova 的 config.xml 文件有关。
以下是我的完整 confix.xml 内容:-
<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
<name>HelloCordova</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<access origin="http://*" />
<access origin="https://*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-intent href="market:*" />
<preference name="loglevel" value="DEBUG" />
</widget>
我的 Ajax 脚本:-
var ajax_url = "http://10.0.2.2/";
jQuery(document).on('pageInit', function (e) {
jQuery.ajax({
url:ajax_url+'api.php/getMenu',
type:'post',
success:function(response)
{
response = jQuery.parseJSON(response);
response = response.payload;
var html = '<ul>';
jQuery(response).each(function(index,value){
htmlpage_name = value.title.replace(/\s/g, '').toLowerCase();
html+='<li>';
if (!/^(f|ht)tps?:\/\//i.test(value.link)) {
html+='<a href="'+htmlpage_name+'.html" class="item-link"><div class="item-content"> <div class="item-inner"> <div class="item-title">'+value.title+'</div></div></div></a>';
}
else
{
html+='<a href="'+value.link+'.html" class="item-link"><div class="item-content"> <div class="item-inner"> <div class="item-title">'+value.title+'</div></div></div></a>';
}
html+='</li>';
});
html+='<li><a href="barcodescan.html" class="item-link"><div class="item-content"> <div class="item-inner"> <div class="item-title">Barcode Scan</div></div></div></li>';
html+='</ul>';
jQuery(".home-page-menu").html(html); }
}); });
我尝试过的事情:-
1) 在confix.xml 文件中添加访问来源。
<access origin="*" />
<access origin="http://*" />
<access origin="https://*" />
2) 在我进行 ajax 调用的 php 文件中添加了标头。
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST,GET,OPTIONS");
3) 在index.html 中添加了Content-Security-Policy 元标记。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
请帮我整理一下。
【问题讨论】:
-
尝试添加一个带有 * 值(或只是 ip)的 connect-src 部分,或者将 * 放在 default-src 中
标签: jquery ajax cordova html-framework-7