来自 Cordova 的 REST 服务和 WAMP(本地)

REST service from Cordova with WAMP (local)

我正在尝试使用 WAMP(在本地)在 Web 服务(由我的 PrestaShop 网站准备)中提取产品。

我的代码: index.js

var app = {

  initialize: function() {
    this.bindEvents();
  },

  bindEvents: function() {
    this.onDeviceReady();
  },

  onDeviceReady: function() {
    var password = '';
    var key = 'F51Q7VWPRREG7TA25DEY8UIZT8V79E5V';
    var url = 'http://localhost/Test/prestashop/api/products?PHP_AUTH_USER="password"&ws_key="key"';

    $.ajax({
      type: "GET",
      dataType: "xml",
      url: url,
      success: this.onSuccess(data),
      error: this.onErreur()
      }); 
  },

  onSuccess: function(data) {
    alert("Success");
  },

  onErreur: function() {
    alert("Error");
  }
};

app.initialize();

![在此处输入图片描述][1]

我尝试用我的 IP 替换 localhost,但还没有成功。 我没有提醒 "Success" 或 "Error"。我不明白为什么。我搜索了很多东西,都没有成功。

编辑:

config.xml

<name>webService</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" />

<plugin name="cordova-plugin-whitelist" version="1" />

<allow-navigation href="http://prestashop/api/products?ws_key=F51Q7VWPRREG7TA25DEY8UIZT8V79E5V&output_format=JSON" />

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>

index.html

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' http://prestashop/api/products?ws_key=F51Q7VWPRREG7TA25DEY8UIZT8V79E5V&output_format=JSON data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" href="bower_components/ratchet/dist/css/ratchet.min.css" media="all">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Web Service</title>
</head>

<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
        <button id="btnCharge" class="btn btn-primary btn-block">Charger Web Service</button>

    </div>

    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
    <script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</body>
</html>

index.js

var app = {
  initialize: function() {
    this.bindEvents();
  },

  bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
  },

  onDeviceReady: function() {
    app.receivedEvent('deviceready');

    $('#btnCharge').click(function(e){
      //alert("btnCharge");
      checkWebService();
    });
  },

  receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
  }
};

app.initialize();




function checkConnection() {
  var networkState = navigator.connection.type;

  var states = {};
  states[Connection.UNKNOWN]  = 'Unknown connection';
  states[Connection.ETHERNET] = 'Ethernet connection';
  states[Connection.WIFI]     = 'WiFi connection';
  states[Connection.CELL_2G]  = 'Cell 2G connection';
  states[Connection.CELL_3G]  = 'Cell 3G connection';
  states[Connection.CELL_4G]  = 'Cell 4G connection';
  states[Connection.CELL]     = 'Cell generic connection';
  states[Connection.NONE]     = 'No network connection';

  //alert('Connection type: ' + states[networkState]);
}


function checkWebService() {
  checkConnection();
  var networkState = navigator.connection.type;

  if (networkState == Connection.NONE) {
    alert("Pas de connexion");
  } 
  else  {

    var key = 'F51Q7VWPRREG7TA25DEY8UIZT8V79E5V'; //http://prestashop/api/products?ws_key=F51Q7VWPRREG7TA25DEY8UIZT8V79E5V&output_format=JSON
    var url = 'http://prestashop/api/products?ws_key=' + key + '&output_format=JSON';

    $.ajax({
      url: url,
      dataType: 'json',
      type: 'GET',
      success: function (products) {
        alert("Success GET JSON" + products);
      },
      error: function (jqXHR, exception) {
        if (jqXHR.status === 0) {
          alert('Not connect.\n Verify Network.');
        } else if (jqXHR.status == 404) {
          alert('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
          alert('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
          alert('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
          alert('Time out error.');
        } else if (exception === 'abort') {
          alert('Ajax request aborted.');
        } else {
          alert('Uncaught Error.\n' + jqXHR.responseText);
        }
      },
    });
  }
}

我用我的浏览器尝试了 index.html 和 index.js 并且它有效。

我也遇到过同样的问题。

我同意@Keval,你可能没有访问互联网的权限。

添加此插件 https://github.com/apache/cordova-plugin-whitelist 并转到您的 config.xml 文件。 在这一行中,只需填写这一行,当然还有您的 IP 地址:

<allow-navigation href="http://xxx.xxx.xxx.xxx/*" />

这可能会解决问题,因为 <access origin="*" /> 还不够。 (至少,对我来说还不够)

为了解决我的问题(将 Android 模拟器连接到 Wamp(本地)),我按照本教程进行操作:http://www.bradcurtis.com/hosts-files-and-the-google-android-emulator/