Ionic3 with woocommerce rest api having post reque

2020-02-07 07:54发布

问题:

I am new to both woocommerce and Ionic and am trying to create an application with woocommerce as backend I have implemented the get request based rest apis in project but am unable to implement the apis with the post request.the function I tried to write for post request to woocommerce create order api is as follows.

  postOrder(postparams){
        var headers = new Headers();
        headers.append("Accept", 'application/json');
        headers.append('Content-Type', 'application/json' );
        headers.append('consumer_key', 'the key comes here');
        headers.append('consumer_secret', 'the secret comes here');
        let options = new RequestOptions({ headers: headers });
        return this.http.post(this.wc_api_path,postparams,options).map(res => res.json());
  }

but this does not seems to work.

I have already referred to This tutorial This Doc This Doc but am unable to understand the issue and implement the api.it would be great if it could be explained with an example.

回答1:

This is the way CORS works (when using cross domain requests). With CORS, the remote Web application (here the one with domain mydomain.org) chooses if the request can be served thanks to a set of specific headers.

The CORS specification distinguishes two distinct use cases:

Simple requests. This use case applies if we use HTTP GET, HEAD and POST methods. In the case of POST methods, only content types with the following values are supported: text/plain, application/x-www-form-urlencoded and multipart/form-data.

Preflighted requests. When the ‘simple requests’ use case doesn’t apply, a first request (with the HTTP OPTIONS method) is made to check what can be done in the context of cross-domain requests. It's not Angular2 that sends the OPTIONS request but the browser itself. It's not something related to Angular.

For more details, you could have a look at this article:

http://restlet.com/blog/2015/12/15/understanding-and-using-cors/