No 'Access-Control-Allow-Origin' header is present upon Ionic2 http post request

前端 未结 2 836
孤城傲影
孤城傲影 2020-12-06 03:42

This is my full code...

this.http.post(link, data, { headers: headers })
   .map(res => res.json())
   .subscribe(data => {
       this.data.response          


        
相关标签:
2条回答
  • 2020-12-06 04:22

    Download the Allow-Control-Allow-Origin application from google chrome. Enable the CORS in the application installed and execute your code. This will temporarily allow the CORS in your browser.

    0 讨论(0)
  • 2020-12-06 04:37

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

    ionic.config.json

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

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

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

    0 讨论(0)
提交回复
热议问题