ionic3 Woocommerce https仅在iOS上发生CORS错误

时间:2018-03-21 10:50:38

标签: ionic3 woocommerce-rest-api

我不知道他们用离子3或角4做了什么,但pff ...

我的问题是在Android上它可以工作,但在iOS设备上,我收到此错误:

Failed to load resource: Request header field password is not allowed by Access-Control-Allow-Headers

这是来电者:

var optionsWoo = new HttpHeaders()
  //.set("Content-Type", "application/x-www-form-urlencoded")
  //.set("Content-Type", "application/json")
  .set("user", wp_key)
  .set("password", wp_sec)
  .set("Authorization", "Basic " + window.btoa(wp_key+":"+wp_sec));

this.http.get(site_url + 'wp-json/wc/v2/customers/' + id, {headers: optionsWoo}).toPromise()
        .then(usr => {
            return this.mergeUser(usr);
        }, err => {
            console.warn('errorS getUserAndMerge', err);
            return Promise.reject(err);
        });

我已经建立了我的app拳头:

 import {HTTP} from '@ionic-native/http'; - in app.module.ts
 import {Http} from "@angular/http"; - in Provider

现在我搬到了:

import {HttpClientModule} from "@angular/common/http";  - in app.module.ts
import {HttpClient, HttpHeaders} from "@angular/common/http"; - in Provider

使用带有离子插件的两个包:

<plugin name="cordova-plugin-advanced-http" spec="^1.11.0" />

httpS身份验证只需添加到标头作为用户/传递API密钥和Api密钥即可。对??我的意思是它是从PostMan开始工作的,它正在开发ANDROID!但是在iOS上我得到了相同的错误...现在已经持续了2天......

离子信息

cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.19.1
ionic (Ionic CLI) : 3.19.1

全球套餐:

cordova (Cordova CLI) : 7.1.0 

本地包裹:

@ionic/app-scripts : 3.1.8
Cordova Platforms  : ios 4.5.4
Ionic Framework    : ionic-angular 3.9.2

系统:

ios-deploy : 1.9.2 
ios-sim    : 5.0.11 
Node       : v8.9.3
npm        : 5.7.1 
OS         : macOS Sierra
Xcode      : Xcode 9.2 Build version 9C40b 

的package.json

{
"name": "myTitlte",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  },
  "dependencies": {
    "@angular/common": "5.2.9",
    "@angular/compiler": "5.2.9",
    "@angular/compiler-cli": "5.2.9",
    "@angular/core": "5.2.9",
    "@angular/forms": "5.2.9",
    "@angular/http": "5.2.9",
    "@angular/platform-browser": "5.2.9",
    "@angular/platform-browser-dynamic": "5.2.9",
    "@ionic-native/app-version": "^4.5.3",
    "@ionic-native/camera": "^4.5.3",
    "@ionic-native/core": "4.6.0",
    "@ionic-native/device": "^4.5.3",
    "@ionic-native/device-feedback": "^4.5.3",
    "@ionic-native/dialogs": "^4.5.3",
    "@ionic-native/http": "^4.6.0",
    "@ionic-native/keyboard": "^4.5.3",
    "@ionic-native/network": "^4.5.3",
    "@ionic-native/splash-screen": "4.6.0",
    "@ionic-native/status-bar": "4.6.0",
    "@ionic-native/zbar": "^4.5.3",
    "@ionic/storage": "2.1.3",
    "cordova-ios": "^4.5.4",
    "cordova-plugin-advanced-http": "^1.11.0",
    "cordova-plugin-app-version": "^0.1.9",
    "cordova-plugin-camera": "^4.0.2",
    "cordova-plugin-cszbar": "^1.3.4",
    "cordova-plugin-device": "^2.0.1",
    "cordova-plugin-dialogs": "^2.0.1",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.16",
    "cordova-plugin-network-information": "^2.0.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.1",
    "cordova-plugin-velda-devicefeedback": "0.0.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "ionic-angular": "3.9.2",
    "ionic-select-searchable": "^1.0.9",
    "ionicons": "3.0.0",
    "rxjs": "5.5.7",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.20"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.8",
    "typescript": "2.7.2"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-app-version": {},
      "cordova-plugin-camera": {},
      "cordova-plugin-cszbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-velda-devicefeedback": {},
      "cordova-plugin-network-information": {},
      "cordova-plugin-dialogs": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-advanced-http": {}
    },
    "platforms": [
      "ios"
    ]
  }
}

刚刚更新了一些主要软件包,例如"@angular/common": "5.0.3",和npm,但同样的事情......

谢谢!

1 个答案:

答案 0 :(得分:0)

我通过添加wordpress secret和key作为查询参数来修复此问题

var authQuery = '?username=' + wp_key + '&password=' + wp_sec;
this.http.post(site_url + 'wp-json/wc/v2/customers' + authQuery etc...