2017-10-17 58 views
0

我發送一個從我的筆記本電腦的ajax請求到我的api休息在無線網絡(沒有互聯網)。cordova ajax請求本地wifi在筆記本電腦上工作,但不在我的設備上

它運行良好,但是當我嘗試從我的手機設備執行相同操作時,我在我的ajax請求中出現網絡錯誤。

我正在使用android設備進行測試。 我正在用visual studio 2017 com和cordova開發6.3.1 我試圖將我的URI添加到config.xml中,我也有'*'屬性。

你能幫我嗎? 這裏是我的代碼:

(function() { 
"use strict"; 

document.addEventListener('deviceready', onDeviceReady.bind(this), false); 

$('#testAPI').click(function() { 
    alert('test4'); 
    $.ajax 
     ({ 
      type: "GET", 
      url: "http://192.168.0.1/api/rest/v1/value", 
      dataType: 'json', 
      async: false, 
      //data: '{"username": "user", "password" : "password"}', 
      success: function (data) { 
       alert('test3'); 
       alert(data.value); 
       //$('#Temperature').html(parseFloat(data.value).toPrecision(4).toString() + ' °C'); 
       $('#testAPI').text(data.value); 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       alert("Status: " + textStatus); alert("Error: " + errorThrown); 
      }, 
      beforeSend: function (xhr) { 
       alert(username); 
       alert(password); 
       xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password)); 
      } 
     }); 
}); 


function onDeviceReady() { 
    // Gérer les événements de suspension et de reprise Cordova 
    document.addEventListener('pause', onPause.bind(this), false); 
    document.addEventListener('resume', onResume.bind(this), false); 

    // TODO: Cordova a été chargé. Effectuez l'initialisation qui nécessite Cordova ici. 
    //var parentElement = document.getElementById('deviceready'); 
    //var listeningElement = parentElement.querySelector('.listening'); 
    //var receivedElement = parentElement.querySelector('.received'); 
    //listeningElement.setAttribute('style', 'display:none;'); 
    //receivedElement.setAttribute('style', 'display:block;'); 
}; 

function onPause() { 
    // TODO: cette application a été suspendue. Enregistrez l'état de l'application ici. 
}; 

function onResume() { 
    // TODO: cette application a été réactivée. Restaurez l'état de l'application ici. 
}; 
})(); 

我使用白名單的插件和我在這裏config.xml中的一部分:

<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:*" /> 
<access origin="*" /> 
<allow-navigation href="*" /> 
</platform> 
+0

嘗試安裝白名單插件。 – user5091906

回答

0

確定這是內容安全策略的問題。 謝謝

相關問題