【问题标题】:Cordova not making ajax request科尔多瓦没有提出ajax请求
【发布时间】: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


【解决方案1】:

localhost 是“这台计算机”,因此当您从计算机浏览器尝试时,它可以工作。 但是模拟器是运行在你的电脑上的虚拟机,所以localhost就是模拟器本身,而不是你的电脑,而且模拟器里面没有服务器,所以ajax调用失败。

模拟器有一个 IP 指向运行它的计算机的“localhost”,即10.0.2.2 阅读更多关于它的信息here

因此您可以使用该 IP 而不是 localhost,但它无法在计算机浏览器上运行。此外,它不适用于真实设备。

您可以做的最好的事情是使用您机器的本地 IP,它可以在浏览器、模拟器以及与您的计算机位于同一本地网络的任何设备上运行。类似于192.168.1.xx

【讨论】:

  • 在学习新东西之前我不知道这一点,感谢您清楚地解释。我试过了,它不起作用。我很确定我的脚本没有错误,因为当我使用我的 Live 服务器 IP 它运行良好,但不适用于 localhost。我正在向我的问题添加 ajax 脚本,请看一下。
猜你喜欢
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 2015-09-08
  • 2017-05-20
  • 2016-08-11
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
相关资源
最近更新 更多