0

i'm using woo api rest ("woocommerce-api": "1.4.2") in my ionic 3 application. when i send a post request i get this error : enter image description here

code :

  signUp() {
    let customerData = {
      "email": this.newUser.email,
      "first_name": this.newUser.first_name,
      "last_name": this.newUser.last_name,
      "username": this.newUser.username,
      "billing": {
        "first_name": this.newUser.first_name,
        "last_name": this.newUser.last_name,
        "company": "",
        "address_1": this.newUser.billing_address.address_1,
        "address_2": this.newUser.billing_address.address_2,
        "city": this.newUser.billing_address.city,
        "state": this.newUser.billing_address.state,
        "postcode": this.newUser.billing_address.postcode,
        "country": this.newUser.billing_address.country,
        "email": this.newUser.email,
        "phone": this.newUser.billing_address.phone
      },
      "shipping": {
        "first_name": this.newUser.first_name,
        "last_name": this.newUser.last_name,
        "company": "",
        "address_1": this.newUser.shipping_address.address_1,
        "address_2": this.newUser.shipping_address.address_2,
        "city": this.newUser.shipping_address.city,
        "state": this.newUser.shipping_address.state,
        "postcode": this.newUser.shipping_address.postcode,
        "country": this.newUser.shipping_address.country
      }
    };
    if (this.billing_shipping_same) {
      customerData.shipping = customerData.billing;
    }
    this.WooCommerce.postAsync("customers",customerData).then(
        (data)=>{
            console.log(JSON.parse(data.body));
        }
    );
  }

but when I send the post request using postman it work also get requests work correctly, I have the problem only in the post!

TheLordOne
  • 65
  • 10

1 Answers1

0

When you try a post method call using angular in chrome it will call the options service so that your api call will through error. using find the below link you can get clear clarification on that.

AngularJS performs an OPTIONS HTTP request for a cross-origin resource

If you try using real time device Your api call will work like a charm try it in real time device.

Manoj Vj
  • 86
  • 4
  • thanks you , the link is for angularJS any one for v5 ? – TheLordOne Sep 19 '18 at 09:42
  • https://stackoverflow.com/questions/36353532/angular2-options-method-sent-when-asking-for-http-get this link is for angular 2 it is also applicable for u I think. – Manoj Vj Sep 19 '18 at 09:43
  • in the link i don't find how use it with woo api : this.WooCommerce.post("customers",customerData,function(err, data, res){ console.log(data); }); – TheLordOne Sep 19 '18 at 10:25
  • the problem is not with woo api it is basically angular http call. Did you check that in real time device – Manoj Vj Sep 19 '18 at 10:31
  • yes that's what i am saying it will be working in postman and real time devices only in browsers the options request will not allow you call the api. Do get the build and run it in the real time device. – Manoj Vj Sep 19 '18 at 10:41
  • I try it in real device, it works, but I want to see results directly in google chrome ? – TheLordOne Sep 19 '18 at 16:24
  • I try all the solution in the post and also I try a chrome plugins but the problem still happen – TheLordOne Sep 20 '18 at 15:29
  • I too exactly don't know how to disable the options request I'm using the native http plugin for the api call. https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it try this link it may be helpful for you. – Manoj Vj Sep 21 '18 at 06:17
  • the problem is I can't add headers to woo api, it doesn't have any function like addHeader : this.WooCommerce.postAsync("customers",data).then((data2) => { console.log(data2); }).catch((err) => { console.log(err); }); – TheLordOne Sep 21 '18 at 08:00