如何通过React Native将图像发布到WordPress

时间:2018-09-28 04:27:00

标签: wordpress rest react-native

我的英语不好,希望你能理解我。 我正在尝试通过ReactNative将媒体文件发布到WordPress。 但是,当我发送请求时,总是会失败。 当我获取帖子和其他数据时还可以吗? 你能帮我解决吗? 谢谢大家:D

那是我的代码:

upload = async ()=>{
    const base64 = require('base-64');
    const data = new FormData();
    data.append('photo','filename');
    data.append('photo', {
        url: this.state.uriImage,
        type: this.state.type,
        name: this.state.fileName
    });
    const headers = new Headers();
    headers.append("Authorization", "Basic " + base64.encode("admin:admin"));
    await fetch('http://nhom2.dotplays.com/wp-json/wp/v2/posts/', {
        method: 'POST',
        headers: headers,
        body: data,
    }).then(res => {console.log(res)})
}

那失败了

允许使用要求周期,但可能导致未初始化的值。考虑重构以消除循环的需要。 blob:http://localhos…-c81bf1f94a97:59031可能的未处理承诺拒绝(id:0): TypeError:网络请求失败 TypeError:网络请求失败

1 个答案:

答案 0 :(得分:-1)

尝试向您添加捕获,它应该为您提供有关帖子失败原因的更多信息,但是当您通过POST到达端点时,我认为这是服务器错误。

//your previous code.....
await fetch('http://nhom2.dotplays.com/wp-json/wp/v2/posts/', {
    method: 'POST',
    headers: headers,
    body: data,
}).then(res => {console.log(res)})
.catch((error) =>{
    // add this and see what you get in the console 
    console.error(error);
});