gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:22:29 24 4
gpt4 key购买 nike

我只是想在我的 IONIC 2 项目中进行 http post 调用。当我运行 ionic serve 时,它工作正常。但是当我运行 ionic run 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;
}
}
}

为了克服这个问题,我尝试了几种方法,比如

  1. 运行cordova plugin add 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 设备点击 post 调用时,我收到以下错误响应:

{"_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"}

我有以下 ionic 信息:

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

谁有解决办法?

提前致谢。

最佳答案

你能试试这个例子吗?您确定要发送正确的 header 吗

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);
});
});
}

关于android - 在 Ionic 2 中进行 HTTP POST 调用会给出禁止响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40886886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com