2017-07-21 19 views
3

我想在restfull api上發佈數據,但我拿(XMLHttpRequest無法加載https://deneme.com/api/v1.0/users/login/。對預檢請求的響應沒有通過訪問控制檢查:沒有'訪問控制-Allow-Origin'標題出現在請求的資源上,因此'http://localhost:8100'不允許訪問。ionic 2沒有'Access-Control-Allow-Origin'頭文件

我用chrome插件(Allow-Control-Allow-Origin:*)解決了這個問題,名稱是交叉的。但我的代碼不適用於Android設備。如何解決這個問題的代碼

我的代碼;

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

此鏈接可能會有幫助https://stackoverflow.com/questions/37599655/no-access-control-allow-origin-header-is-present-on-the-requested-resource-i – hrdkisback

+0

嘗試'讓標題= new Headers({'Content-Type':'application/x-www-form-urlencoded'});'。刪除所有其他標題。 –

+0

添加您的config.xml和您正在使用的插件列表。 –

回答

0

嘗試添加允許控制允許原產地在RESTAPI的文件上的文件的頂部

0

使用頭參數是這樣的:

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

希望工程。