我目前正在制作一個移動應用程式,它從 reddit API 獲取資料并將其顯示在 Reddit 界面的重制版上。
該應用程式由多個頁面組成,第一個頁面是登錄頁面,它使用 oauth 2.0 來檢索連接令牌,然后導航到另一個頁面,我嘗試獲取有關我的 reddit 個人資料的資料并顯示它。
登錄頁面按縮進方式作業,我可以連接到 reddit API 并導航到下一頁,但是當我嘗試從 reddit API 獲取任何資料時,我收到了405錯誤代碼,有時會出現500錯誤。
這是第二頁后端的代碼:
const LoginScreen = ({navigation}) => {
let token = route.params.token;
const [username, setUsername] = useState('');
const [sub_count, setSubCount] = useState('');
const [profile_img_url, setImgUrl] = useState('');
const [date, setDate] = useState('');
async function fetchRedditAPI() {
var config = {
method: 'set',
url: 'https://oauth.reddit.com/api/v1/me',
headers: {
'Authorization': 'Bearer ' token
}
};
axios(config)
.then(function (response) {
console.log("----API DATA-----")
console.log(response["data"])
console.log("-----------------")
let profile_img_url = response["data"]["icon_img"]
setImgUrl(profile_img_url);
let date = response["data"]["created_utc"]
setDate(date);
let username = response["data"]["display_name_prefixed"]
setUsername(username);
let sub_count = response["data"]["subscribers"]
setSubCount(sub_count);
})
.catch(function (error) {
console.log("GOT THE FOLLOWING ERROR :")
console.log(error);
console.log("------------------------")
});
}
React.useEffect(() => {
console.log("fetching reddit API...");
fetchRedditAPI();
}, []);
我的終端中顯示以下錯誤:
Request failed with status code 405
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
Request failed with status code 500
at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:605:6 in setReadyState
at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
任何幫助都是寶貴的,在此先感謝您!
uj5u.com熱心網友回復:
一個405 當您嘗試訪問使用不允許的請求方法端點出現錯誤。我認為這是因為您的請求方法set不是get?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/343485.html
