Ionic app can't connect to local wordpress site


#1

Hello,
i have a local wordpress site and i created an ionic app that must connect to the wordpress site (api) request and display data. but i keep getting access error on console. someone sugested to create a proxy in angular. but i don’t know how to do that.


#2

Use localhost server like Xampp? i build app with WordPress and ionic on localhost work fine. Paste service code here.


#3

thanks for your reply. im using MAMP.

var WooCommerceAPI = require(‘woocommerce-api’);

@Component({
selector: ‘page-about’,
templateUrl: ‘about.html’
})
export class AboutPage {

  woocommerce: any;
  constructor(public navCtrl: NavController) {

        this.woocommerce =  WooCommerceAPI({

          url: 'http://localhost:8888/wordpress/',
          consumerKey: 'ck_79ca76.......',
          consumerSecret: 'cs_82466.........'

        });

this.woocommerce.getAsync(“products”).then((data)=>{
console.log(“PASSED”);
console.log(JSON.parse(data));

},(err)=>{
console.log(“ERROR”);
console.log(err);
});

  }

}

This is the error i get

No ‘Access-Control-Allow-Origin’ header is present on the requested resource.


#4

That’s a CORS error. Have you added proxies for your local/MAMP urls?

e.g. in your ionic.config.json file:

{
  "name": "YourAPP",
  "app_id": "123456",
  "type": "ionic-angular",
  "proxies": [
    {
      "path": "/login",
      "proxyUrl": "http://localhost:1234/api_folder/"
    }
  ],
  "integrations": {
    "cordova": {}
  }
}

#5

thanks for your reply. but i still have the same errors even though i made the change you suggested. here is my ionic.config.json file:

{
“name”: “mytabApp”,
“app_id”: “”,
“type”: “ionic-angular”,
“proxies”: [
{
“path”: “/login”,
“proxyUrl”: “http://localhost:8888/wordpress/wc-api/v3/
}
],
“integrations”: {
“cordova”: {}
}
}

my app run at

http://localhost:8101


#6

try changing

{
“path”: “/login”,
“proxyUrl”: “http://localhost:8888/wordpress/wc-api/v3/”
}

to

{
“path”: “/login”,
“proxyUrl”: “http://localhost:8888/wordpress/wc-api/v3/login”
}

maybe?


#7

i try to change as you suggested still no success