我知道這是一個重復的問題,但我仍然需要對我的代碼進行幫助。
當我用postman發出一個post請求時,它成功了,但當我使用flutter代碼時,它失敗了。
有什么想法嗎?
因此,這是flutter的代碼,用于發布請求:
這是flutter的代碼,用于發布請求。
Future createQuote() async {
final response = await http.post(
Uri.parse('http://<myserver.com> /quotes/post.php')。
body: json.encode(
{
'quot': _quoteController.text,
'teller': _tellerController.text,
},
),
);
if (response.statusCode == 200 && response.body == 'success') {
print('s: ' response.body);
//Navigator.pop(context);.
} else {
print(response.body)。
var test = jsonEncode(
{
'quot': _quoteController.text,
'teller': _tellerController.text,
},
);
print(test)。
// throw Exception('Failed to create quote');
}
}
而這是php檔案:
require_once('db.php'/span>)。
$stm = $db->prepare("INSERT INTO quots (quot, teller) VALUES (:quot, :teller)" )。
$stm->bindParam(':quot', $_POST['quot'])。)
$stm->bindParam(':teller', $_POST['teller']) 。
$quot = $_POST['quot'] 。
$teller = $_POST['teller']。
if ($stm-> execute()) {
echo "success"。
} else {
echo "失敗。"。$_POST['quot'/span>] 。$teller;
};
uj5u.com熱心網友回復:
沒有必要對帖子主體進行jsonEncode,將其作為一個普通的Map使用。
final response = await http.post(
Uri.parse('http://<myserver.com> /quotes/post.php')。
body:
{
'quot': _quoteController.text,
'teller': _tellerController.text。
},
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320465.html
標籤:
