【问题标题】:Uncaught (in promise): Response with status: 401 Unauthorized for URL: http://example.com/wp-json/wp/v2/posts未捕获(承诺):响应状态:401 未经授权的 URL:http://example.com/wp-json/wp/v2/posts
【发布时间】:2018-08-13 04:40:46
【问题描述】:

我正在尝试使用 ionic 中的 Angular 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

看看 netwokr 调试怎么说:

直接从浏览器访问http://example.com/wp-json/wp/v2/posts会返回预期的结果。

【问题讨论】:

    标签: angularjs wordpress ionic-framework wordpress-rest-api


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 2022-08-07
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多