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
我已经尝试了其他类似问题中提到的所有选项,例如下面提到的那些。
有人可以解决这个问题吗? TIA。
答案 0 :(得分:0)
要解决CORS问题,我建议您使用此扩展程序,因为在浏览器上运行时只会出现此类错误。
Allow-Control-Allow-Origin:*
删除您为解决CORS问题所做的一切,并尝试再次运行您的程序。
答案 1 :(得分:0)
在服务器上,在请求的网址的根目录中创建一个包含此内容的“.htaccess”文件:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"
答案 2 :(得分:0)