0

I'm using ionic3 to build E-commerce app with Wordpress as a backend. I'm using Woocommerce api to get products, orders, ect. this my code to configure it

import * as WC from "woocommerce-api";
Woocommerce = WC({
    url: this.url,
    consumerKey: this.consumerKey,
    consumerSecret: this.consumerSecret,
    wpAPI: true,
    queryStringAuth: true,
    version: "wc/v2"
  });
 let query = "products?" + "page=" + this.page;
    if (this.selected != "") query = "products?page=" + this.page;
    query = query + "&status=publish" + "&" + this.config.productsArguments;
    this.config.Woocommerce.getAsync(query).then((data) => {
        console.log(data)
    })

It worked on android Well but on Ios i have this error :

https://testsite.com/wp-json/wc/v2/products?include=2047%2C1828%2C1768%2C1767%2C1766%2C1765%2C1763&status=publish&order=desc&orderby=date&lang=en&currency=EGP&consumer_key=ck_xxxxxxxxxxxxxxxx&consumer_secret=cs_xxxxxxxxxxxxxxz Failed to load resource: Origin httpsionic://localhost is not allowed by Access-Control-Allow-Origin.

Nazeeh
  • 45
  • 1
  • 10

1 Answers1

0

This is not a WordPress issue but rather a CORS (Cross-Origin Resource Sharing) issue. You may refer here: https://www.joshmorony.com/dealing-with-cors-cross-origin-resource-sharing-in-ionic-applications/.

As per summary, you may deal with it in a couple of ways: first, disable origin checking in your API host (WordPress) Access-Control-Allow-Origin: * in headers.

Second, you may proxy requests through native code. Check this plugin https://ionicframework.com/docs/native/http/.

Earlee
  • 338
  • 4
  • 17
  • I use woocommerce-api but this how i use native http with it. – Nazeeh Sep 17 '20 at 17:58
  • I see. The WooCommerce Rest API JS Lib, https://github.com/woocommerce/woocommerce-rest-api-js-lib uses Axios. The issue is similar here https://stackoverflow.com/questions/50949594/axios-having-cors-issue – Earlee Sep 18 '20 at 23:54