Ionic 2 No "Access-Control-Allow-Origin" header

I want to post data to a restfull api, but I take (XMLHttpRequest cannot load https://deneme.com/api/v1.0/users/login/ . The response to the preflight request does not pass the access control check: there is no header in the requested resource "Access-Control-Allow-Origin". Hence, Origin ' http: // localhost: 8100 ' is not allowed access.) Error.

I solved this problem with the chrome plugin (Allow-Control-Allow-Origin: *). but my code doesn't work on android device. how can i solve this problem with code

my code;

kanallistele()
{

  var veri;

     this.kanallardiv=true;
    this.uyegirisdiv=false;
    this.kanallistelebtn = false;
    var headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append('Content-Type', 'application/json' );
    headers.append('Access-Control-Allow-Origin','*'); 
    //headers.append('Authorization' , 'Basic '+ btoa(tok));
    let options = new RequestOptions({ headers: headers });

    let postParams = {
    token: "381f13d7056-ce5fe474919",
    user_id: "71",
    }

    var veris="channel_name_";
    this.http.post("https://deneme.com/api/v1.0/channels/", postParams, options)
      .subscribe(data => {
        veri = data['_body'];
        console.log(veri);       
        veri= veri.slice(1, -1);
        veri = veri.replace(/\\/g, "");
        veri = JSON.parse(veri);
        for(var i = 0 ;;i++)
          {
              if(!veri.channel_list['channel_name_' + (i)])
                    break;   

              this.veriler.push({channelname: veri.channel_list['channel_name_' + (i)],channelid: veri.channel_list['channel_id_' + (i)]});              
          }
       }, error => {
        console.log(error);// Error getting the data
      });  
  }

      

+3


source to share


2 answers


try adding Allow-Control-Allow-Origin to Restapi file on top of files



0


source


Use the header parameter like this:

let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.headers = {headers};
this.http.post(API_URL, JSON.stringify(userData), this.headers).map(res => 
res.json());

      



Hope it works.

0


source







All Articles