Ionic ios应用程序无法通过livereload访问API

时间:2016-01-03 01:25:51

标签: ionic-framework cors

使用ionic emulate ios,可以很好地访问我本地计算机上的API。但是,ionic emulate ios -l -c,应用的API请求只能以预检请求的形式到达服务器:

OPTIONS /api/user/get/profile_mob 200 1.68 ms

ionic.project使用代理:

"proxies": [
    {
      "path": "/api",
      "proxyUrl": "https://dev.app.com/api"
    }
 ]
下面

nginx.conf

location /api {
  add_header              'Access-Control-Allow-Origin' '*';
  add_header              'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE';
  add_header              'Access-Control-Allow-Credentials' 'true';
  add_header              'Access-Control-Allow-Headers' 'X-Requested-With,content-type';
  proxy_redirect          off;
  proxy_http_version      1.1;
  proxy_set_header        Host $http_host;
  proxy_set_header        X-Scheme $scheme;
  proxy_set_header        X-Real-IP $remote_addr;

  proxy_set_header        Upgrade     $http_upgrade;
  proxy_set_header        Connection  "upgrade";

  proxy_hide_header       X-Powered-By;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass              http://localhost:3003;
}

从我的阅读中,配置代理和添加代理应该已经解决了问题。我还能尝试什么?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你没有提到,使用了什么插件..! 可能这可以帮助你..!

1:添加此插件

cordova add plugin cordova-plugin-whitelist

2:检查config.xml中是否添加了<access origin="*" />

3:在index.html中添加“启用所有请求,内联样式和eval()”的CSP,如here

所示

以及更多信息,请参阅this ..和that ..

答案 1 :(得分:0)

我为我们找到了一个解决方案,我们有一个api验证了cors的起源,它只承认来自localhost或127.0.0.1的请求,我使用了代理方法但没有工作,然后使用--address param它工作好的,在设备和emulador上重新加载,我们有两个nom任务,我希望这有帮助:

"emulate:ios:debug": "ionic emulate ios --consolelogs --livereload --debug --address 127.0.0.1 --target=\"iPhone-6s\""

"device:ios:debug": "ionic prepare ios --device && ionic run ios --consolelogs --livereload --debug --address 127.0.0.1"

http://ionicframework.com/docs/cli/test.html