Ionic2 http post 请求中不存在“Access-Control-Allow-Origin"标头 [英] No 'Access-Control-Allow-Origin' header is present upon Ionic2 http post request

查看:26
本文介绍了Ionic2 http post 请求中不存在“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的完整代码...

this.http.post(link, data, { headers: headers })
   .map(res => res.json())
   .subscribe(data => {
       this.data.response = data._body;
    }, error => {
        console.log("Oooops!");
    });

运行代码后出现此错误:

after running the code this error is present:

"XMLHttpRequest cannot load 
https://script.google.com/macros/s/AKfycbzdHHKBmLWJYZtFGlJGOrUwlPIWXor1geEOgcSgvhs/dev.     
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8100' is therefore not allowed access. 
The response had HTTP status code 401."

我已经搜索了 CORS...但我无法理解它...

I've searched about CORS... but I can't get my head around it...

任何帮助将不胜感激.

推荐答案

我有同样的问题,但经过几个小时的搜索,我的问题消失了.

i have same issue but after some hours to search my problem gone.

ionic.config.json

ionic.config.json

{
  "name": "KickStarter",
  "app_id": "85ff0666",
  "v2": true,
  "typescript": true,
  "proxies": [
    {
      "path": "/mobile",
      "proxyUrl": "http://xxxxx:port/mobile"
    }
  ]
}

你应该使用 ionic g provider [name-of-provider] --ts 它会生成提供者来发出这样的请求:

you should use ionic g provider [name-of-provider] --ts it will generate provider to make a request like this:

export class AuthProvider {
    data: any = null;

    constructor(public http: Http) { }

    load() {
        if (this.data) {
            // already loaded data
            return Promise.resolve(this.data);
        }

        // don't have the data yet
        return new Promise(resolve => {
            // We're using Angular Http provider to request the data,
            // then on the response it'll map the JSON data to a parsed JS object.
            // Next we process the data and resolve the promise wi new data.
            this.http.get('/mobile/api/authentication')
                .map(res => res.json())
                .subscribe(data => {
                    // we've got back the raw data, now generate the core schedule data
                    // and save the data for later reference
                    resolve(this.data);
                });
        });
    }
}

只要记住:/mobile/api/authentication -> /mobile 来自 path in ionic.config.json.

just remember: /mobile/api/authentication -> /mobile from path in ionic.config.json.

这篇关于Ionic2 http post 请求中不存在“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆