具有代理的离子调用API始终返回404

时间:2017-10-09 10:57:15

标签: angular ionic-framework ionic2

我遇到问题访问API,网址始终返回外部IP: http://192.168.**.**:8100

我的API托管在本地IIS服务器上,例如此IP 192.168.**.**:3000

当我在Android模拟器上调试项目时,我在调试控制台上看到了:

  

Proxy added:/api => http://192.168.**.**:3000/

我使用此方法访问api。但我的网址总是回来 192.168.**.**:8100/api/Configurations/GetAppConfiguration所以我收到错误404,因为我的API IP地址使用3000端口。

我添加了config.xml允许导航href我的API网址http://192.168.**.**:3000,但它没有用。

有什么问题?我错过了什么?

谢谢你的帮助......

GetMethod

getPosts (): Observable<any[]> {
    debugger;
    return this.http.get("api/Configurations/GetAppConfiguration")
        .map(this.parseData)
        .catch(this.handleError);
}

Ionic.config.json

{
  "name": "myApp",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {
    "cordova": {}
  },
  "proxies": [
    {
      "path": "/api",
      "proxyUrl": "http://192.168.** .** :3000"
    }
  ]
}

launch.json

 "name": "Run Android on emulator",
            "type": "cordova",
            "request": "launch",
            "platform": "android",
            "target": "emulator",
            "port": 9222,
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "ionicLiveReload": true,
            "devServerTimeout": 120000

的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>MyApp</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content original-src="index.html" src="http://192.168.**.**:8100" />
    <access origin="*" />
    <allow-navigation href="http://ionic.local/*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <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" />
    <preference name="SplashScreen" value="none" />
    <platform name="android">
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />   
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <icon height="57" src="resources/ios/icon/icon.png" width="57" />      
    </platform>
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="SplashScreenDelay" value="3000" />
    <allow-navigation href="http://192.168. ** . ** :8101" />
    <allow-navigation href="http://192.168. ** . ** :8100" /> 
    <engine name="android" spec="^6.2.3" />
    <plugin name="cordova-plugin-device" spec="^1.1.6" />
    <plugin name="cordova-plugin-network-information" spec="^1.3.3" />
    <plugin name="cordova-plugin-screensize" spec="^1.3.1" />
    <plugin name="cordova-plugin-splashscreen" spec="^4.0.3" />
    <plugin name="cordova-plugin-statusbar" spec="^2.2.3" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.2" />
    <plugin name="ionic-plugin-keyboard" spec="^2.2.1" />
</widget>

1 个答案:

答案 0 :(得分:2)

我解决了这个问题

添加 ionic.config.json 文件我的代理设置如下

"proxies": [
      {
          "path": "*/apibase",
          "proxyUrl": "http://192.168. ** . **:3000"
      }
   ]

更改Config.xml文件并添加新的配置属性

<allow-navigation href="http://*/*" />
<allow-intent href="https://*/*" />

使用像这样的get方法

getPosts (): Observable<any[]> {        
 return this.http.get("apibase/api/Configurations/GetAppConfiguration")
    .map(this.parseData)
    .catch(this.handleError);

}