Cors:可以获取但不能发布

时间:2020-06-26 05:08:40

标签: laravel api ionic-framework cors

我通过Ionic React开发应用程序。

API是使用Laravel制作的。我的响应头中有200个状态的好数据。

Access-Control-Allow-Origin *
Access-Control-Allow-Credentials true
Access-Control-Allow-Methods GET, POST, PUT, DELETE, OPTIONS
Client side :

static getStat(): Promise<any>{
    return fetch(`http://192.168.1.250/bodt78/public/api/index`,{
        method: 'GET',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then((response) => {
        if(response.status === 200){
            return true
        }
        else{
            return false
        }
    })
    .catch(error => this.handleError(error));
}

这是一个完美的作品……但是当我想请求登录(POST)

return fetch(`http://192.168.1.250/bodt78/public/api/auth/login`,{
      method: 'POST',
      headers: { 'Content-Type': 'application/json',
                  'Access-Control-Allow-Origin': 'http://localhost:8100',
                },
      body: JSON.stringify({
      "email": email,
      "password": password}),
    }).then((res) => {
      console.log(res);
      if (res.status == 200) {
        return res.json();
      } else {
        if (res.status == 401) {
          return res.status;
        } else {
          return null;
        }
      }

    });

每次尝试获取时...我都有一个错误:(

[Error] Origin http://localhost:8100 is not allowed by Access-Control-Allow-Origin.
[Error] Fetch API cannot load http://192.168.1.250/bodt78/public/api/auth/login due to access control checks.

我用以下命令编辑ionic.config.json:

  "proxies": [{
    "path": "/bodt78/public/api",
    "proxyUrl": "http://192.168.1.250"
}]

你有什么主意吗?我真的尝试了一切!

@sideshowbarker可以节省您的时间并去做其他事情,而不是禁止其他人只是为了提高您的答案等级!

谢谢

0 个答案:

没有答案