Using an authorization header with Fetch in React Native

前端 未结 4 753
天涯浪人
天涯浪人 2020-11-27 10:18

I\'m trying to use fetch in React Native to grab information from the Product Hunt API. I\'ve obtained the proper Access Token and have saved it to State, but d

4条回答
  •  一向
    一向 (楼主)
    2020-11-27 11:02

    completed = (id) => {
        var details = {
            'id': id,
    
        };
    
        var formBody = [];
        for (var property in details) {
            var encodedKey = encodeURIComponent(property);
            var encodedValue = encodeURIComponent(details[property]);
            formBody.push(encodedKey + "=" + encodedValue);
        }
        formBody = formBody.join("&");
    
        fetch(markcompleted, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: formBody
        })
            .then((response) => response.json())
            .then((responseJson) => {
                console.log(responseJson, 'res JSON');
                if (responseJson.status == "success") {
                    console.log(this.state);
                    alert("your todolist is completed!!");
                }
            })
            .catch((error) => {
                console.error(error);
            });
    };
    

提交回复
热议问题