【问题标题】:Ionic - HTTP Post working on serve and -l but not working when run on android deviceIonic - HTTP Post 在serve 和-l 上工作,但在android 设备上运行时不起作用
【发布时间】:2018-08-21 15:25:32
【问题描述】:
let headers = new Headers({ 
    'Content-Type' : 'application/json'
});

let options = new RequestOptions({ headers: headers });
this.http.post('https://cors-anywhere.herokuapp.com/'+url, body, options)
  .subscribe(data => {
    let response = JSON.parse(data['_body']);
    if (response.MessageCode == 'SUCCESS') {
      this.loginMessage = 'success';
      this.navCtrl.push(HomePage, response);
    }
    else {
      this.loginMessage = 'failure';
    }

   }, error => {
    console.log(error);
  });

我遇到了 CORS 问题。所以,我在 URL 之前使用它来使它工作。

下面是config.xml

<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:*" />
<preference name="ScrollEnabled" value="false" />
<preference name="android-minSdkVersion" value="16" />
<preference name="BackupWebStorage" value="none" />
<preference name="SplashMaintainAspectRatio" value="true" />
<preference name="FadeSplashScreenDuration" value="300" />
<preference name="SplashShowOnlyFirstTime" value="false" />
<preference name="SplashScreen" value="screen" />
<preference name="SplashScreenDelay" value="3000" />
<plugin name="cordova-plugin-splashscreen" spec="^5.0.2" />
<plugin name="cordova-plugin-ionic-webview" spec="^1.1.16" />
<plugin name="cordova-plugin-ionic-keyboard" spec="^2.0.5" />
<plugin name="cordova-plugin-device" spec="^2.0.1" />
<plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
<plugin name="cordova-plugin-advanced-http" spec="^1.11.0" />

请求标头-

Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: 64.77.72.122
Origin: http://localhost:8100

响应标头-

Connection: close
Content-Length: 1245
Content-Type: text/html
Date: Thu, 15 Mar 2018 15:37:44 GMT
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET

我已经尝试了其他类似问题中提到的所有选项,例如下面提到的问题。

this

有人可以解决这个问题吗? TIA。

【问题讨论】:

  • 您能否包含您看到的一整套请求和响应标头,用于预检 OPTIONS 请求(如果有)以及您尝试发出的 GET/POST 请求.
  • 已编辑。请看一看。
  • 没关系。修复。它与服务器端和正文参数有关。感谢您的宝贵时间!

标签: typescript http ionic-framework cors cordova-plugins


【解决方案1】:

要解决 CORS 问题,我会推荐这个 Chrome 扩展,因为在浏览器上运行时只会出现这种错误。

Allow-Control-Allow-Origin: *

删除您为解决 CORS 问题所做的所有操作并尝试再次运行您的程序。

【讨论】:

  • 该扩展程序将为您完成。你只需要安装它。
  • 我试过那个 chrome 扩展。没用。但是在 ionic serve 和 ionic run livereload 期间将cors-anywhere.herokuapp.com 附加到 url 中起作用。需要解决这个问题。
【解决方案2】:

在您的服务器上,在请求的 URL 的根目录中创建一个包含以下内容的“.htaccess”文件:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"

【讨论】:

    【解决方案3】:

    这是一个服务器端正文参数问题。

    CORS 只是在 Web 浏览器上进行测试时出现的问题。

    可以通过启动浏览器来处理here。无需使用任何 chrome 扩展或插件来处理它。

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 2015-08-12
      • 2015-05-17
      相关资源
      最近更新 更多