【问题标题】:IONIC - HTTP Post request not working, problems with headers maybeIONIC - HTTP Post 请求不起作用,可能是标头问题
【发布时间】:2020-01-13 13:13:12
【问题描述】:

我的 POST 请求无法联系本地 API,本地 API 与 get 请求一起工作没有问题

public PostChat(club: string, id, rep:string) {
        console.log("begin");
        let headers = new Headers(
        {
            'Content-Type' : 'application/json',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': '*'
        });
        let options = new RequestOptions({ headers: headers });

        let data = JSON.stringify({
            club : club,
            id: id,
            rep: rep
        });

        return new Promise((resolve, reject) => {
            this.http.post(`${this.baseUrlChat}senduser.php`, data, options)
            .toPromise()
            .then((response) =>
            {
                console.log('API Response : ', response.json());

            })
            .catch((error) =>
            {
                console.error('API Error : ', error.status);
            });
        });
    }

实际错误信息:

访问 XMLHttpRequest 在 'http://192.168.43.58/api/chat/senduser.php' 来自原点 'http://localhost:8100' 已被 CORS 策略阻止:请求 头字段 access-control-allow-origin 不允许 预检响应中的 Access-Control-Allow-Headers。

【问题讨论】:

  • 这不是 Ionic 问题,它是您的服务器上的问题,不允许跨源响应

标签: angular ionic-framework ionic3


【解决方案1】:

您需要在后端处理 CORS 以接受跨源请求。

像这样:

<?php
 header("Access-Control-Allow-Origin: *");

通过这个Article

【讨论】:

  • 已经有......从我的应用程序到我的 API 的所有获取请求都工作正常,只是 post 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 2023-03-28
相关资源
最近更新 更多