【问题标题】:post request get 401 (Unauthorized) woo rest api发布请求获取 401(未经授权)woo rest api
【发布时间】:2018-09-18 14:46:45
【问题描述】:

我在我的 ionic 3 应用程序中使用 woo api rest ("woocommerce-api": "1.4.2")。 当我发送发布请求时,我收到此错误:

代码:

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

但是当我使用邮递员发送帖子请求时,它也可以正常工作,我只在帖子中遇到问题!

【问题讨论】:

    标签: angular ionic-framework ionic3 angular5 woocommerce-rest-api


    【解决方案1】:

    当您尝试在 chrome 中使用 angular 调用 post 方法时,它会调用选项服务,以便您的 api 调用将通过错误。使用找到下面的链接,您可以得到明确的说明。

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

    如果您尝试使用实时设备您的 api 调用将像一个魅力一样在实时设备中尝试。

    【讨论】:

    • 谢谢,这个链接是 angularJS v5 的任何一个?
    • stackoverflow.com/questions/36353532/… 此链接适用于 Angular 2,我认为它也适用于您。
    • 在链接中我找不到如何将它与 woo api 一起使用:this.WooCommerce.post("customers",customerData,function(err, data, res){ console.log(data) ; });
    • 问题不在于 woo api,它基本上是有角度的 http 调用。你检查了实时设备吗
    • 是的,这就是我所说的,它将仅在浏览器中的邮递员和实时设备中工作,选项请求将不允许您调用 api。获取构建并在实时设备中运行它。
    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多