未捕获(承诺):回复状态:401未经授权的URL:http://example.com/wp-json/wp/v2/posts

问题描述 投票:0回答:1

即时尝试使用角度js promise在离子中从我的wordpress网站获取json数据,使用以下代码:

    import { Injectable } from '@angular/core';
import {Headers, Http} from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = 'http://example.com/wp-json/wp/v2/posts';

/*
  Generated class for the NewsProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class NewsProvider {

  constructor(public http: Http) {
    console.log('Hello NewsProvider Provider');
  }


    getPosts(){

    return new Promise((resolve, reject) =>{
            let headers = new Headers();
            this.http.post(apiUrl, {headers: headers}).
            subscribe(res =>{
                resolve(res.json());
            }, (err) =>{
                reject(err);
            });

        });

    }
}

现在调用此方法时,我收到以下错误:

未捕获(承诺):回复状态:401未经授权的URL:http://example.com/wp-json/wp/v2/posts

并听听网络调试说:qazxsw poi

enter image description here

并直接从浏览器访问enter image description here返回预期的结果。

angularjs wordpress ionic-framework wordpress-rest-api
1个回答
0
投票

我的问题来自this.http.post(apiUrl,{headers:headers})。

我应该使用get而不是post.http.get(apiUrl,{headers:headers})。

© www.soinside.com 2019 - 2024. All rights reserved.