2016-04-20 59 views
0

我在IONIC 2應用程序中對我的服務器發出請求時遇到問題。IONIC2,Angular 2設備上的Http請求錯誤 - 訪問文件://改爲http://

在ionic.config.json我已經設置了代理(我休息API是直接從http://SERVER_ADDRESS.pl速效):

"proxies": [{ 
    "path": "/rest", 
    "proxyUrl": "http://SERVER_ADDRESS.pl" 
}] 

在我的TS文件我有:

this.http.get(`/rest/getmenu`) 
.map(res => res.json()) 
.subscribe(res => { 
     this.menus = res; 
     console.log(this.menus); 
    }, 
    err => { 
     console.log(err); 
     this.error = err; 
    } 
); 

在配置.XML我說:

<allow-navigation href="*" /> 

當我用「離子服務」一切都很好,但是當我在emual開始運行我有錯誤。當我調試Chrome上,我可以看到錯誤:

無法加載資源:淨:: ERR_FILE_NOT_FOUND文件:///休息/使用getMenu

如何解決?

我已經安裝了cordova-plugin-whitelist(cordova-plugin-whitelist 1.2.3-dev「Whitelist」)。 我的config.xml:

<?xml version='1.0' encoding='utf-8'?> 
<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <name>V2 Test</name> 
    <description>An Ionic Framework and Cordova project.</description> 
    <author email="[email protected]" href="http://ionicframework.com/">Ionic Framework Team</author> 
    <content src="index.html" /> 
    <access origin="*" /> 
    <allow-navigation href="*" /> 
    <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> 
    <preference name="webviewbounce" value="false" /> 
    <preference name="UIWebViewBounce" value="false" /> 
    <preference name="DisallowOverscroll" value="true" /> 
    <preference name="android-minSdkVersion" value="16" /> 
    <preference name="BackupWebStorage" value="none" /> 
    <feature name="StatusBar"> 
     <param name="ios-package" onload="true" value="CDVStatusBar" /> 
    </feature> 
</widget> 

和離子信息:

Cordova CLI: 4.3.1 
Gulp version: CLI version 3.9.0 
Gulp local: Local version 3.9.1 
Ionic Framework Version: 2.0.0-beta.4 
Ionic CLI Version: 2.0.0-beta.25 
Ionic App Lib Version: 2.0.0-beta.15 
OS: Distributor ID: Ubuntu Description: Ubuntu 14.04.4 LTS 
Node Version: v4.0.0 

回答

2

代理設置是在瀏覽器中只有http://ionicframework.com/docs/cli/test.html測試。

由於ionic serve啓動了一個web服務器,它也可以創建一個代理。當你部署到手機時,沒有網絡服務器,只是文件夾中的文件,所以沒有代理服務器。您需要直接點擊URL或運行您的應用可以訪問的代理服務器。

+1

是的,你是對的。如果我在模擬器或設備上使用完整地址 - 它可以工作。另外,我可以在「離子服務」上禁用CORS。 – IceManSpy