javascript - react native 使用fetch 向后端傳數(shù)據(jù)提示錯(cuò)誤
問題描述
我的react native 代碼如下:
fetch(’https://www.lisonblog.cn/apptest/test.php’,{ methos: ’POST’, headers: {’Content-Type’: ’application/json’, }, body: JSON.stringify({postData:’lalala’ })}).then(function(res) { alert(res.status) if (res.status === 200) {return res.json() } else {return Promise.reject(res.json()) }}).then(function(data) { alert(data)}).catch(function(err) { alert(err);});
我的后端的PHP代碼是這樣的:
<?php $res = $_POST[’postData’]; echo $res;?>最后在手機(jī)APP上彈出這個(gè)錯(cuò)誤:TypeError:Body not allowed for GET or HEAD requests
請(qǐng)問是什么原因?我看網(wǎng)上有的需要傳輸?shù)臄?shù)據(jù)是formData類型的,求大神賜教
問題解答
回答1:第2行,應(yīng)該是method,不是methos
回答2:直接原因是由于 ‘method’ 拼寫錯(cuò)誤。背后原因是由于拼寫錯(cuò)誤導(dǎo)致默認(rèn)請(qǐng)求方式為 ‘GET’ 請(qǐng)求, 二 HTTP/1.1協(xié)議規(guī)范里 對(duì)于 ‘GET’ 請(qǐng)求不支持在 body 里攜帶數(shù)據(jù),參數(shù)只能通過URL傳遞。具體可參考 http://stackoverflow.com/ques...
回答3:應(yīng)該使用postData=’lalala’,跟jquery查找規(guī)則相關(guān),具體可再了解。
相關(guān)文章:
1. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入2. javascript - 關(guān)于apply()與call()的問題3. html - eclipse 標(biāo)簽錯(cuò)誤4. python 利用subprocess庫(kù)調(diào)用mplayer時(shí)發(fā)生錯(cuò)誤5. python - Pycharm的Debug用不了6. datetime - Python如何獲取當(dāng)前時(shí)間7. 請(qǐng)問PHPstudy中的數(shù)據(jù)庫(kù)如何創(chuàng)建索引8. python - pycharm 自動(dòng)刪除行尾空格9. python文檔怎么查看?10. javascript - nginx反向代理靜態(tài)資源403錯(cuò)誤?
