React Native提取返回404但邮递员可以200确认

时间:2019-10-22 07:19:46

标签: react-native get fetch http-status-code-404 spotify

我正在将Spotify Web API集成到React本机应用程序中。我对React Native提取有问题。当我在Postman内部调用api时,会收到200条响应代码,但是如果我在模拟器(react native)中运行它,则会得到404错误代码。我还在chrome控制台中编写了fetch请求,它运行良好,但在本机响应中却不起作用。

我正在运行的代码:

let response = await fetch(`https://api.spotify.com/v1/browse/featured-playlists?locale=en_US&country=US`, {
        method: 'GET',
        headers: {
            'Accept': 'application/json;charset=utf-8',
            'Content-Type': 'application/json;charset=utf-8',
           'Authorization': 'Bearer BQDT3_xtYLyIsGaqhoTM42Z-k3ijs-hFbzIG89basd7Me-kL4SUWSLgQrxWN3b13DiqU6THfr4VH4Z7klnc' 
        }
    });
    console.log("response",response.status)

1 个答案:

答案 0 :(得分:0)

我在React Native v.8.6.5中尝试了以下代码

async componentDidMount() {
    let spotifyApiUrl = "https://api.spotify.com/v1/browse/featured-playlists?locale=en_US&country=US";
    let fetchSettings =  {
        method: 'GET',
        headers: {
            'Accept': 'application/json;charset=utf-8',
            'Content-Type': 'application/json;charset=utf-8',
            'Authorization': 'Bearer BQDT3_xtYLyIsGaqhoTM42Z-k3ijs-hFbzIG89basd7Me-kL4SUWSLgQrxWN3b13DiqU6THfr4VH4Z7klnc' 
        }
    };

    fetch(spotifyApiUrl, fetchSettings)
        .then( (response) => response.json() )
        .then( (responseJson) => {
            console.log(JSON.stringify(responseJson));
        })
        .catch((error) => {
            console.log(error);
        });
}

具有以下控制台输出:

{"error":{"status":401,"message":"The access token expired"}}

您是否尝试过插入实际设备以查看它是否是模拟器问题? 如果您使用的是真实设备,请记住在运行“ react-native run-android”之前在adb上转发tcp端口

adb reverse tcp:8081 tcp:8081
react-native run-android