4

I want to post data on restfull api but I take (XMLHttpRequest cannot load https://deneme.com/api/v1.0/users/login/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.) error.

I solved this problem with chrome plugin (Allow-Control-Allow-Origin: *) name is cross. but my code is not working on android device. how can ı 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
      });  
  }
Ahmet emre CETIN
  • 321
  • 1
  • 4
  • 22

2 Answers2

0

Use 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.

Kishan Oza
  • 1,707
  • 1
  • 16
  • 38
Vikharry91
  • 32
  • 1
-1

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