【问题标题】:Ionic 2 WP-REST API post request 401 error with headers set for basic authenticationIonic 2 WP-REST API 发布请求 401 错误,标头设置为基本身份验证
【发布时间】:2017-02-15 12:26:21
【问题描述】:

我正在开发一个简单的应用程序,通过 wp-rest api 在 Wordpress 中创建帖子。一切(创建,更新删除帖子)在邮递员上都可以正常工作。我什至可以在我的应用程序中使用相同的 api 获取帖子。但是当我尝试创建帖子时,它会在控制台中显示 401 Unauthorized 错误。 这是我的请求代码。

//create post by api
createPost(title,content){
    var headers = new Headers();
    headers.append('Authorization', 'Basic '+btoa('tarun:iamtarun'));
    headers.append("Content-Type", "application/json");
    return this.http.post(this.postUrl+'?title='+title+'&content='+content , {
        headers:headers 
    })
    .map(res => res.json());
}

请帮忙。

【问题讨论】:

    标签: http angular ionic2 basic-authentication wp-api


    【解决方案1】:

    httppost请求的结构是

    post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;

    所以请求应该是,

    return this.http.post(this.postUrl+'?title='+title+'&content='+content ,{}, { headers:headers })
    

    body 是一个空对象。

    【讨论】:

    • 非常感谢。发现。现在像魅力一样工作。
    • 如果有帮助,请接受它作为答案。
    猜你喜欢
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2019-02-26
    • 2014-02-10
    相关资源
    最近更新 更多