2016-06-11 45 views
1

这是我的全code ...否“访问控制允许来源”标头在办理Ionic2 HTTP POST请求

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

运行代码后,这个错误存在:

"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 ...但我不能让我的头周围...

任何帮助将不胜感激。

+0

Duplicate:http://stackoverflow.com/questions/30132885/ionic-app-cannot-connect-cors-enabled-server-with-http – Quentin

+1

相关:http://stackoverflow.com/questions/35553500/xmlhttprequest -cannot-load-https-www-website-com/ – Quentin

+0

你正在发送你的POST请求的链接没有来自服务器的访问权限来访问它请求的任何信息......它是客户端到位的安全机制服务器通信 – repzero

回答

-3

从谷歌浏览器下载Allow-Control-Allow-Origin应用程序。在安装的应用程序中启用CORS并执行您的代码。这将暂时允许您的浏览器中的CORS。

+0

这对于单个用户来说只是一个短期的修复程序 – Mike

+0

它在浏览器中工作... firefox和blisk ...但它不适用于Ionic2开发环境... –

2

我有同样的问题,但几个小时后,搜索我的问题消失了。在ionic.config.json>/mobilepath - /mobile/api/authentication

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它会产生供应商提出请求如下:

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

只记得。

+0

谢谢,它对离子2.0.1 – fifth

+0

@ vuhung3990我很努力使您的解决方案完全理解,因为使用GlobalEnv,AuthBody,Headers,Languages等语言并不能为您提供使用哪些软件包的线索。你能开导我吗?也许还包括一些进口产品。干杯。 – JGFMK

+0

@JGFMK嗨,你不需要知道GlobalEnv,AuthBody,Headers是什么意思,这是我的自定义组件,我只是更新我的答案 – vuhung3990

相关问题