在Ionic 2中进行HTTP POST调用会给出禁止响应

jyo*_*ran 5 android cordova ionic-framework cordova-plugins ionic2

我只是想在我的IONIC 2项目中进行http post调用.当我运行离子服务时它工作正常.但是当我运行离子运行anroid以在我的设备上运行它时它给了我403(禁止)响应.

这是我的PaymentService.ts,其中已经进行了http调用

import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';

@Injectable()
export class PaymentService {
static get parameters() {
    return [[Http]];
}

constructor(private http:Http) {}

postToPaymentApi(data) {
    let headers: Headers = new Headers()
    headers.set('Content-type', 'application/x-www-form-urlencoded')
    let options = new RequestOptions({headers: headers})

    var response = this.http.post("http://test/url",data,options).map(res => res.json());
    return response;
   }
 }
}
Run Code Online (Sandbox Code Playgroud)

为了克服这个问题,我尝试了几件事,比如

  1. 运行cordova插件添加cordova-plugin-whitelist
  2. 在我的config.xml中添加了这一行 <meta http-equiv="Content-Security-Policy" content="default-src *; img-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
  3. 按照此链接 - > https://github.com/apache/cordova-plugin-whitelist添加<access origin>*<allow-navigation>,<allow-intent>标记具有指定属性.
  4. 试图添加proxyUrl,如http://blog.ionic.io/handling-cors-issues-in-ionic/

但就我而言,没有上述解决方案可行.

当我从我的Android设备点击后调用时,我收到以下错误响应:

{"_body":"",
"status":403,
"ok":false,
"statusText":"Forbidden",
"headers":{"Date":["Wed","30 Nov 2016 09:28:53 GMT"],
"Content-Length":["0"]},
"type":2,
"url":"http://test/url"}
Run Code Online (Sandbox Code Playgroud)

我有以下离子信息:

Cordova CLI: 6.4.0 
Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.12
Ionic App Lib Version: 2.1.7
Ionic App Scripts Version: 0.0.45
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.4
Node Version: v7.2.0
Xcode version: Not installed
Run Code Online (Sandbox Code Playgroud)

有解决方案的人吗?

提前致谢.

fla*_*imi 0

你能试试这个例子吗?您确定发送了正确的标头吗

postToPaymentApi(data) {
   let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });

   return new Promise(resolve => {
      this.http.post('http://test/url',data, {headers: headers })
         .subscribe(respond => {
           console.log(respond);       
         });   
   });        
}
Run Code Online (Sandbox Code Playgroud)