我想创建从Woocommerce获取数据的应用程序。 我正在使用离子框架:
$ionic info
ionic (Ionic CLI) : 4.1.1 (C:\Users\hp\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0
Cordova:
cordova (Cordova CLI) : 8.0.0
Cordova Platforms : not available
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 2.1.0, (and 4 other plugins)
System:
NodeJS : v10.9.0 (C:\Program Files\nodejs\node.exe)
npm : 6.2.0
OS : Windows 10
对于Angular:
$ng -v
@angular/animations: 5.2.11
@angular/common: 5.2.11
@angular/compiler: 5.2.11
@angular/compiler-cli: 5.2.11
@angular/core: 5.2.11
@angular/forms: 5.2.11
@angular/http: 5.2.11
@angular/platform-browser: 5.2.11
@angular/platform-browser-dynamic: 5.2.11
首先要检索数据,我首先使用以下命令将woocommerce-api导入我的应用程序:
npm install woocommerce-api@latest
并将其导入到我的home.ts:
import * as WC from "woocommerce-api";
现在在我的构造函数中:
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
WooCommerce: any;
constructor(public navCtrl: NavController) {
this.WooCommerce = WC({
url: "https://example.com",
consumerKey: "ck_3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
consumerSecret: "cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
wpAPI: true, // Enable the WP REST API integration
queryStringAuth: true,
verifySsl: true,
version: 'wc/v2'
});
this.WooCommerce.getAsync("products").then( (data) =>{
console.log(JSON.parse(data.body));
}, (err) => {
console.log(err)
} )
}
}
现在当我使用ionic serve命令运行我的应用程序时,我看不到日志文件中的数据,相反,我看到了XHR错误,我不知道这是什么:
cause: Error
columnNumber: 23
fileName: "http://localhost:8100/build/vendor.js"
lineNumber: 167557
**message: "XHR error"**
stack: "ClientRequest.prototype._onFinish/xhr.onerror@http://localhost:8100/build/vendor.js:167557:23
\nH@http://localhost:8100/build/polyfills.js:3:23948\nF</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
\nF</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
\nF</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16787
\np@http://localhost:8100/build/polyfills.js:2:27646\nv@http://localhost:8100/build/polyfills.js:2:27893\n"
我正在搜索很多东西,但找不到原因。
什么是错误? 如何解决?